Sunday, July 12, 2009

C++ Programmers, need help. would you?

I wrote this program and i compiled it successfully, this program runs successfully, but when it retrieves data from user the program get closed. whats the prob?


Here is my program and my compiler is Dev-C++





# include %26lt;iostream%26gt;





using namespace std;


int main ()


{


float grade;


printf ("Please enter your grade : ");


scanf("%d",grade);


if (grade %26gt;10){


printf("You have passed the test...");


}


else


{


printf("You havent passed the test...");


}


cin.get();


cin.ignore();


return 0;


}

C++ Programmers, need help. would you?
There are two problems:





1. In the scanf the %d means the input is an integer, but you declared the variable as a float.





2. Also in the scanf, you need to pass a reference to the variable, not the variable itself.





So change it to scanf("%f",%26amp;grade);





And it will work for you. %f means float, and %26amp;variable means take the address of (pass by reference.)





Alternatively, you can declare grade as an int, and keep the %d.








--------------------------------------...


Yes, you're going to have a problem with bogus inputs.





You could check the return value of the scanf call.





if !(scanf("%f", %26amp;grade) {


printf("Rerun the program and try putting in a number this time");


return 1;


}
Reply:I am guessing the enter keystroke you pressed to send your number input to the program zipped you past the cin.get statement and returned (ended). If that´s not the case, you can always through in some trace statements and a few getchar()´s at the end (or step through with a debugger).
Reply:In DEV-C++ you need to add, before your return 0; this linke





"system("PAUSE");"
Reply:cin.get will read a white space because it doesn't expect to read anything from the input. If you run your program from cmd line it won't close automatically after running it (because the cmd session is still active).


The easy way would be to request the user to type in a key to continue, something like...


printf("Press any key to continue...");


scanf("%c",%26amp;key)..


This will require to store useless data in your program and of course it can be avoided. The alternative would be to "pause" the system until the user decides to end the session manually. Just type


system("PAUSE");


at the end and it will pause the program until the user press a key to exit.
Reply:what is the program trying to do, i am a C++ programmer, you can contact me at srp333@comcast.net for help--------------


#include %26lt;stdio.h%26gt;


#include %26lt;windows.h%26gt;


int main()


{


char grade[20];


printf("Please enter your grade: ");


gets(grade);


if(grade %26gt; 10)


{


printf("Your have passed the test.\n");


system("pause");


}


else


{


printf("You havent passed the text");


system("pause");


}


}
Reply:I think it's because the scanf doesn't take the "return" key after the grade : it is still in the buffer, so cin.get() catches it at once, and your program closes without waiting.


In C, you can address the problem in a nasty way by putting a "fflush(stdin);" just after your scanf. There are prettier ways of doing it, but I just can't remember them right now.


I guess if you know the representation of the return key in your system (\n, \r\n, \r), you can make scanf catch it for you at the end of its first parameter (but your code may have portability issues).


No comments:

Post a Comment