why does the code ignore my scanf in a switch, loop, if etc statements. How can I get my code to stop and read user input.Code:switch (choice) { case 1: printf("Enter Letter:"); scanf("%c", &letter); printf("Its %c\n",letter); break; }
Thanks
why does the code ignore my scanf in a switch, loop, if etc statements. How can I get my code to stop and read user input.Code:switch (choice) { case 1: printf("Enter Letter:"); scanf("%c", &letter); printf("Its %c\n",letter); break; }
Thanks
What happened before the switch?
More scanf calls perhaps?
scanf just consumes as many characters as it needs, so if you have
scanf("%d", &myint);
scanf("%c",&mychar);
and you type in
123\n
Then myint is 123, and mychar contains '\n'
If you type in
123abc\n
Then myint is 123, and mychar contains 'a', and the bc\n is left for someone else to deal with.
No thats not the problem, sorry I did not explain it properly. When the code enters the switch it does not wait at all for user input therefore declaring NULL and goes on to display
Enter Letter: Its
I find it rather odd. It executes as a block of statements as it should do without waiting on any user input.
Salem explained the problem.
How do you get choice?
I'd guess...
So you press 1 then <Enter>Code:scanf("%d", &choice);
1 is consumed by your call to scanf and the newline character is left in the stdin buffer.
Your second call to scanf then consumes the newline chracter as it's looking for a character (%c). It's done it's job at that point, read one character from stdin. It doesn't wait for more input since a character was already availible.
print letter immediately after the scanf, you should see it prints a newline
Last edited by spydoor; 01-09-2006 at 12:54 PM.
Hi Spydoor what a bizarre way of doing it though but this code worked.
Then with this while loop to clear the buffer after the scanfCode:fflush(stdout);
This worked but is their a better way of doing it, maybe an input that clears automatically!!Code:while((c = getchar()) != EOF && c != '\n') { ; }
Yes, use fgets() to read all input into a buffer.
Then use sscanf(), or some other conversion which works on data in memory, to extract the data which interests you.
Does fflush() clear the errors from the specified stream?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
No, it doesn't: http://www.cplusplus.com/ref/cstdio/fflush.html
So you'd be better off using
instead/as well.Code:clearerr(stdout);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.