scanf("%d",&age);
When the execution of the program reaches the above line,you type an integer and press enter.
The integer is taken up by scanf
and the \n
( newline character or Enter )which you have pressed remains in the stdin
which is taken up by the getchar()
.To get rid of it,replace your scanf
with
scanf("%d%*c",&age);
The %*c
tells scanf
to scan a character and then discard it.In your case,%*c
reads the newline character and discards it.
Another way would be to flush the stdin
by using the following after the scanf
in your code:
while ( (c = getchar()) != '\n' && c != EOF );
Note that c
is an int
in the above line