PDA

View Full Version : Help with scanf in 'C' coding


Devaspora
10-14-2009, 05:21 PM
printf("question1? (y)es or (n)o?\n");
gets(xx);
printf("\n");
switch (xx[0])
{
case 'y': printf("yes reply\n\n");
break;
case 'n': printf("no reply\n\n");
break;
default: printf("default\n\n");
}

printf("Question2? (y)es or (n)o?");
gets(xx);
printf("\n");
switch (xx[0])
{
case 'y': printf("yes reply\n\n");
break;
case 'n': printf("no reply\n\n");
break;
default: printf("default\n\n");
}

printf("Question3? (y)es or (n)o?\n");
gets(xx);
printf("\n");
switch (xx[0])
{
case 'y': printf("yes reply\n\n");
break;
case 'n': printf("no reply\n\n");
break;
default: printf("default\n\n");
}

This is my code, and the problem is that only Question 1 doesnt allow you to input anything and automatically goes to default. Would anybody happen to know why this is?

Also this is so extra info:
int cats;
char o, w, a, g, h, c, x, y, n, xx[5],xxx;

oracleguy
10-14-2009, 05:31 PM
The code you posted works fine for me.

Also in regards to this:
char o, w, a, g, h, c, x, y, n, xx[5],xxx;

The only people you are punishing by using super short variable names is yourself if you come back to the program later and anyone else that needs to read your code. Use longer, more descriptive variable names, it can help a lot when debugging your program.

tulasi
10-21-2009, 04:45 PM
---------------------
Hi friend

Use fflush(stdin) before scanf this should work fine

Regards
prasad