enum fivenums {Z,O,T,TH,F,FI}
int main()
{
char ch;
int num;
do{
scanf("%d",&num);
switch(num)
{
case Z:
printf("ZERO\n");
break;
case O:
printf("ONE\n");
break;
case T:
printf("TWO\n");
break;
case TH:
printf("THREE\n");
break;
case F:
printf("FOUR\n");
break;
case FI:
printf("FIVE\n");
break;

default :
printf("Invalid\n");
break;
}
getchar();// to eat '\n' terminator so that it waits for below 
}
while ((ch =getchar())=='\n');
return 0;
}

in the above program if i input a number between 0 -3 its working fine.
but if the input is any alphabet i am getting a different out put;

for example:
my input is
1
o/p: ONE
2
o/p: TWO
and if its
a O/P is:
TWO
if i give a its giving the previously read input as out put
i.e 2.
and for any other alphabets also its giving the same o/p.
why is it giving the previous one.
when its already read from the buffer.

Recommended Answers

All 2 Replies

its because in line #7 u are reading using scanf() and its looking for an int as input. When u enter a non-digit character it doesn't get what it is expecting hence it won't set the value to the variable num.
Try giving a character in the very first time and u will see the difference. It will go to the default clause of the switch statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.