Hi all,
i came across this problem when i was trying to code sorting of strings posted in happycodingc.blogspot.com...
Scenario:
In the first occurence of scanf()when we enter data to it we tap ENTER key ,and this remains in the STDIN,when we encounter the progressive input functions like Scanf ,fgetc and fgets, then the input to it will be a NEWLINE character as the value in the STDIN is still unread and when the second input function tries to read from the STDIN and it will read '\n' due to enter key and this is not expected...sp we can avoid this by using getchar()in and fflush(stdin)in c++.
check the example
also try uncommenting the getchar();
int main()
{
int iTest;
char cTest;
scanf("%d",&iTest);
// getchar();
scanf("%c",&cTest);
}
No comments:
Post a Comment