Your problem comes that after  scanf, the file pointer don't point at the end of stdin.
it points to the first caracter scanf did not return (if you pressed "abc\n" your getchar will return '\n')

you can solve the problem easely by adding
    fseek(stdin,0,SEEK_END);
before using getchar()
 

by the way the solution
    while ((num=getchar())<=32); // do nothing
is a buggy one because if the user enter
"ABC D"   (note the space between the C and the D)
you will get 'D'. It also don't authorize the user to enter ' ' (and I don't see any reason for that)
 
 

By the way, I think some people here under estimate programming. If we had more programming
question/answer maybe we would already have a good hebrew support.
 
 

Meidan Zemer wrote:

Hi,
I have a small C program wich i compile with GNU C Compiler 2.8.1 and 2.7.
and i got the same result in both times.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main()
{
 char str[10];
 int num;
 scanf("%s",str);
 printf("scanf accept %s\n",str);
 num=getchar();
 printf("getchar accept %c\n",num);
}

For some reason "getchat()" accept Null string and do'nt  prompt for the user.
I run  kernel 2.2.3 on pentium 120 with 32MG
I am sure it is somting simple but i just can'nt see it!!
Does anybody have any idea about it?
Thank you

Reply via email to