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
using namespace std;
int main ()
{
float grade;
printf ("Please enter your grade : ");
scanf("%d",grade);
if (grade >10){
printf("You have passed the test...");
}
else
{
printf("You havent passed the test...");
}
cin.get();
cin.ignore();
return 0;
}
2007-03-30
11:09:41
·
6 answers
·
asked by
Anonymous
in
Computers & Internet
➔ Programming & Design
I just added fflush(stdin); to after the scanf, but the prob is when i input a letter it returns the else clause
2007-03-30
11:30:59 ·
update #1
and numbers are still got problem
2007-03-30
11:31:39 ·
update #2
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",&grade);
And it will work for you. %f means float, and &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", &grade) {
printf("Rerun the program and try putting in a number this time");
return 1;
}
2007-03-30 11:20:37
·
answer #1
·
answered by Vegan 7
·
1⤊
0⤋
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).
2007-03-30 18:16:43
·
answer #2
·
answered by Eusebius 3
·
0⤊
2⤋
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",&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.
2007-03-30 18:25:44
·
answer #3
·
answered by agent-X 6
·
0⤊
1⤋
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).
2007-03-30 18:21:12
·
answer #4
·
answered by rentaprogrammer 2
·
0⤊
1⤋
what is the program trying to do, i am a C++ programmer, you can contact me at srp333@comcast.net for help--------------
#include
#include
int main()
{
char grade[20];
printf("Please enter your grade: ");
gets(grade);
if(grade > 10)
{
printf("Your have passed the test.\n");
system("pause");
}
else
{
printf("You havent passed the text");
system("pause");
}
}
2007-03-30 18:15:34
·
answer #5
·
answered by SRP333 2
·
0⤊
1⤋
In DEV-C++ you need to add, before your return 0; this linke
"system("PAUSE");"
2007-03-30 18:51:51
·
answer #6
·
answered by josh.weissbock 3
·
0⤊
0⤋