I am a beginner. I am learning c. The problem occurs when I have more than one getchar(). (or getc or fgetc etc...) When the program reaches the next getchar (or getc or fgetc..) it seems like it just skips over it, you type something, hit <enter> and its keeps going, skipping over the next getchar(). I tried to figure it out on my own and I learned that its retaining the <enter> keystroke and putting it as the first character of the next variable (or buffer) I intended to use.
fflush() and fpurge() wont work, no matter what parameters I use,what combinations, or where I place them in the program. fflush(stdout) wont flush the '\n', I heard its not a good idea to fflush(stdin), and fpurge(stdin) is killing the buffer before I can use it. gcc returns exact same results. Also, I compiled it on other platforms with different compilers.....I still get the same thing.
I have a "go-arounds" but I would like to know whats really going on.
Here...check this out....
Can someone break this down for me? Maybe from a stdin, stdout kind of point of view?
thanks
fflush() and fpurge() wont work, no matter what parameters I use,what combinations, or where I place them in the program. fflush(stdout) wont flush the '\n', I heard its not a good idea to fflush(stdin), and fpurge(stdin) is killing the buffer before I can use it. gcc returns exact same results. Also, I compiled it on other platforms with different compilers.....I still get the same thing.
I have a "go-arounds" but I would like to know whats really going on.
Here...check this out....
Code:
//----------------------- testbuf.c ----- cc -Wall -g -o testbuf testbuf.c
#include <stdio.h>
void myrout()
{
char a;
printf("\nEnter another key : ");
a = getchar();
printf("\nYou entered : %c\n",a);
}
int main()
{
char b;
printf("\nEnter a key : ");
b = getchar();
printf("\nYou entered : %c\n",b);
myrout();
return 0;
}
Can someone break this down for me? Maybe from a stdin, stdout kind of point of view?
thanks