Click to See Complete Forum and Search --> : Why doesn't it work?


bigrigdriver
10-24-2002, 01:43 PM
language: c

objective: get user to enter one character from the keyboard, and evaluate the character (determine if it's upper or lower case, digit, whitespace, etc).



int main (void)

{

/*local definitions*/

char local_char;

int doIt;


/*statements*/
do

{

printf ("do you want to enter a character? 1 = y; 0 = n");

scanf("%d", &doIt);



if (doIt == 1)

{

printf("type a character and press enter");

scanf("%c", &local_char);

get_input (&local_char);

} /* end block if */

} while (doIt == 1) /* end do-while */

return 0;

} /* main */



The first scanf works (stops and waits for user to choose to continue or quit).

The second scanf (inside the if block) doesn't stop program execution and wait for user to input a character from the keyboard. Execution skips over the scanf, and goes directly to the function call to analyse the keyboard input, which is supposed to be passed by address to the function get_input.

Question: what would cause gcc to ignore the second scanf?

Stuka
10-24-2002, 04:17 PM
What's happening is that when you type the character and then hit enter, the scanf is grabbing the character, but leaving the return in the buffer. Your next scanf grabs that without thinking. One way of pulling that return character out is to call getchar(). (Of course, that would work for all your inputs in this program, so you might want to look into it.)
<edit>gets() may work better for you too, but it's hard to say - take a look at your options, and figure out how to get that return character out of there</edit>

bigrigdriver
10-24-2002, 05:09 PM
Thank, you, Stuka!


Earlier today I read documentation that discussed flushing the buffer to avoid problems of leftovers that could be read by scanf. I didn't see the connection. I think I'll just "#define FLUSH" the thing and add the requisite line further on down and see what happens. Thanks again.

Stuka
10-25-2002, 10:20 AM
You're quite welcome - this is something we all ran into when learning console I/O in C.

mingshun
10-25-2002, 11:26 AM
Another way is to use fflush (0); after the scanf

Stuka
10-25-2002, 11:51 AM
Actually, that's not correct. From the man page for fflush:The function fflush forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. The open status of the stream is unaffected. fflush is for output streams, not input streams. It may work - today, on this particular C library. Tomorrow, or with a different implementation, it may not.

bigrigdriver
10-29-2002, 03:45 AM
"#define FLUSH while getchar () != '\n'" fixed the problem.
As to using fflush to do the same in the input stream, according to my instructor (and the textbook), it is possible if you fflush the input stream thusly: fflush(stdin). That works if the input stream is the standard input stream. If not, just substitute the name for the input stream in use: i.e. filename in double quotes if reading from a file, etc.

bwkaz
10-29-2002, 10:53 AM
Originally posted by bigrigdriver
according to my instructor (and the textbook), it is possible if you fflush the input stream thusly: fflush(stdin). That works if the input stream is the standard input stream. Sorry, but your instructor and the textbook are wrong. Like Stuka said, it may work, today, with the C library that you're using. But the C libraries specifically do not have to support it, and many of them (especially for other OS'es, where stream I/O is much different from file I/O) don't.