Why does scanf sometimes fails like in this example the 2nd scanf was ignored.
include <stdio.h>
#include <conio.h>
main()
{
int x;
char y;
float a;
scanf("%i",&x);
scanf("%c",&y);
scanf("%f",&a);
printf("%i %c %f",x,y,a);
}
Thanks
Why does scanf sometimes fails like in this example the 2nd scanf was ignored.
include <stdio.h>
#include <conio.h>
main()
{
int x;
char y;
float a;
scanf("%i",&x);
scanf("%c",&y);
scanf("%f",&a);
printf("%i %c %f",x,y,a);
}
Thanks
Isn't scanf in a differen't header file?
Also, at the mercy of any potential replies to your message, please use code tages and make sure your code's syntax is exactly what you're trying to compile.
The world is waiting. I must leave you now.
It is because scanf() reads the enter key after you enter first input number. it reads the number and puts it into x but then it goes on to read that enter key stroke and puts it into c. Then when you enter a char it will try to put it into the float value.
you should do this:
scanf("%i ",&x);
scanf("%c",&y);
scanf("%f",&a);
note the space after the %i in the first scanf. That will eat the enter key for you so it will work right.
Dohojar Moajbuj
Time is the greatest teacher, too bad it kills all its students
I think that Shadow is correct, the only header file I see being properly used is <conio.h>.
He doesn't even need conio.h. He is using all stdio.h functions.
Another way to fix the scanf problem is to flush the stdin buffer after each reading so the next scanf will have a fresh, empty buffer:
Good luck!Code:#include <stdio.h> int main() { int i; char c; float f; scanf("%d", &i); fflush(stdin); scanf("%c", &c); fflush(stdin); scanf("%f", &f); fflush(stdin); printf(" %d - %c - %f ", i, c, f); return 0; }
When reading string data, scanf does take a newline character as valid input, so when you used scanf the last time for numeric data the newline was left in the buffer and the next call to scanf that accepted a newline as valid scanned it in.
A newline is also considered whitespace by scanf, which is delimited by whitespace. So it appeared as if the call was skipped when it was in fact working properly.
>Another way to fix the scanf problem is to flush the stdin buffer after each reading
>fflush(stdin);
Wrong, using fflush in input streams results in undefined behavior which is a big no-no. I'll try to be nice, but please do not answer questions if all you can teach is bad habits biosx, I've already corrected you on this point several times. Here's how to flush the input stream safely:
-PreludeCode:#include <stdio.h> int main ( void ) { int i, c; scanf("%d", &i); while ( getchar() != '\n' ); scanf("%c", &c); printf("Integer: %d\nCharacter: %c\n", i, c); return 0; }
My best code is written with the delete key.
Can I still use fflush function if the case is just an alternative to getch() function, cause if Ill be using linux OS conio.h library is not available.
can I use
assuming
char dummy;
fflush(stdin);
scanf("%c",&dummy);
instead of getch();
Thanks prelude
>Can I still use fflush function if the case is just an alternative to getch() function
Here's the thing, if fflushing input streams is undefined in one case then it's undefined in ALL cases. Don't use fflush ( stdin ), don't use it ever. You can use fflush as long as it's on output streams though.
If I had the choice of using something nonstandard, using something standard but not quite what I want, and using something undefined then I would go with standard. Undefined is never to be used, nonstandard only when you must, and standard whenever you can.
-Prelude
My best code is written with the delete key.
Can I use then getchar as an alternative as you have given previously? Is this function also available in linux OS for C.
Thanks
Prelude
>Can I use then getchar as an alternative as you have given previously?
Yes
>Is this function also available in linux OS for C
getchar() is a standard function. If you search www.google.com then you can find a list of the standard functions. Linux has nothing to do with the availability of functions, that is the sole duty of the compiler.
-Prelude
My best code is written with the delete key.