Originally Posted by
ulti-killer
2) isdigit is not working as scanf("%d") only read the digit and skips the char/string.
Maybe i wasn't clear enough.Sorry for that :/
First the input matter.
Assume that i want to read two user's inputs..First input,then print ,then input again and then print again..So i have to scanf,then printf, then scanf again and then printf for last time.
So i could do something like this..
Code:
#include <stdio.h>
int main(void)
{
char choice,anotherChoice;
printf("Please type c , h or i\n");
scanf("%c",&choice);
if(choice == 'c') {
printf("User typed c\n");
}
else if(choice == 'h') {
printf("User typed h\n");
}
else if(choice == 'i') {
printf("User typed i\n");
}
printf("Please type c , h or i\n");
scanf("%c",&anotherChoice);
if(anotherChoice == 'c') {
printf("User typed c\n");
}
else if(anotherChoice == 'h') {
printf("User typed h\n");
}
else if(anotherChoice == 'i') {
printf("User typed i\n");
}
return 0;
}
And the output
Code:
Macintosh-c8bcc88e5669-9:~ usi$ gcc -Wall px.c -o px
Macintosh-c8bcc88e5669-9:~ usi$ ./px
Please type c , h or i
c
User typed c
Please type c , h or i
Macintosh-c8bcc88e5669-9:~ usi$
What is the problem?That code "skipped" my second scanf, so without the program to wait for user to input for second time it terminated bypassing the user's input.So what happened?
I press a character and then i hit enter.So now the buffer of the stdin has the character and the newline character.scanf will read the character but the newline character stays there waiting to get read.So the next scanf has already data to process and that it is what is doing.It reads the newline character, does not find a match in the if/else that follow and reaches return 0 of main function..
Now if we look into the documentation of scanf we will find out that if we leave a space between the double quotes and the percentage operator, then scanf will "eat" the newline character will is exactly what we want
So in the code that i posted above i only modify the second scanf ,leaving the space i quoted above.The result is
Code:
scanf(" %c",&anotherChoice);
Now the output is this
Code:
Macintosh-c8bcc88e5669-9:~ usi$ pico px.c
Macintosh-c8bcc88e5669-9:~ usi$ gcc -Wall px.c -o px
Macintosh-c8bcc88e5669-9:~ usi$ ./px
Please type c , h or i
h
User typed h
Please type c , h or i
i
User typed i
Macintosh-c8bcc88e5669-9:~ usi$
Alternative solution.We know that a newline character is in the stdin buffer, but we do not want it to be handled as user's input.Thus we can instead of modifying the second scanf to write a line of code with getchar (or equivalently getc(stdin) ) that will eat this newline character.Then the unmodified second scanf will do what we want to.
In code (intermediate a getchar and leave the second scanf as it was in the first piece of code)
Code:
#include <stdio.h>
int main(void)
{
char choice,anotherChoice;
int c;
printf("Please type c , h or i\n");
scanf("%c",&choice);
if(choice == 'c') {
printf("User typed c\n");
}
else if(choice == 'h') {
printf("User typed h\n");
}
else if(choice == 'i') {
printf("User typed i\n");
}
c = getchar();
/* Newline's ascii code is 10 */
if( c == 10 )
printf("\nNewline was indeed in the stdin buffer before 2nd scanf\n\n");
printf("Please type c , h or i\n");
scanf("%c",&anotherChoice);
if(anotherChoice == 'c') {
printf("User typed c\n");
}
else if(anotherChoice == 'h') {
printf("User typed h\n");
}
else if(anotherChoice == 'i') {
printf("User typed i\n");
}
return 0;
}
Output :
Code:
Macintosh-c8bcc88e5669-9:~ usi$ gcc -Wall px.c -o px
Macintosh-c8bcc88e5669-9:~ usi$ ./px
Please type c , h or i
c
User typed c
Newline was indeed in the stdin buffer before 2nd scanf
Please type c , h or i
h
User typed h
Macintosh-c8bcc88e5669-9:~ usi$
Oh also do fell into the trap of fflush . When i first encountered this problem , everybody said flush the stdin...
This leads to unexpected behavior... I suggest you read this short lines of explanation here .
Now , the isdigit and the isalpha
.. actually the links are pretty good..so check them and if you have questions ask
I also stated the fgets function which is highly recommended by me.Notice in the link what is stated about newline character.
It will return to you to an array all the input given.This is very useful, because it is like you have parsed the input.
Example
Code:
#include <stdio.h>
int main(void)
{
char buf[10];
int i;
printf("Please type an alpharithmetic input\n");
fgets(buf , 10 , stdin);
for( i = 0 ; i < 10 ; i++)
{
if(buf[i] == '\0' || buf[i] == '\n')
break;
printf("buf[%d] = %c\n",i,buf[i]);
}
return 0;
}
Output
Code:
Macintosh-c8bcc88e5669-9:~ usi$ pico px.c
Macintosh-c8bcc88e5669-9:~ usi$ ./px
Please type an alpharithmetic input
sam1
buf[0] = s
buf[1] = a
buf[2] = m
buf[3] = 1
Macintosh-c8bcc88e5669-9:~ usi$
If you get a number in the input, it is very possible to want its value.For this operation use atoi
Also in case you find yourself in meeting gets function , be kind with her and say bb .
The reason is here .
Hope this helps