Need some more C programming help
I've been making some strides in learning C, and for some reason I cannot make a simple switch statement work properly. I've tried writing two different programs that are simple letter-guessing games.
Every time, you answer the first question properly, it sends you to question 2 and then directly to the default: statement.
Answering the first question incorrectly displays the default: statement twice before dropping you back to the first question.
Any help would be immensely appreciated.I'm new at this, so please forgive my ignorance and mistakes.
[code]
#include <stdio.h>
int main()
{
char q1_input = 0;
char q2_input = 0;
short main_count = 0;
printf("This is a very simple letter-guessing quiz");
Question1:
{
printf("\
\
What letter am I thinking of?");
scanf("%c", &q1_input);
switch (q1_input)
{
case 'C': case 'c':
printf("\
Excellent! You guessed right!");
main_count += 5;
goto Question2;
break;
default:
printf("\
Sorry, try again!");
goto Question1;
break;
}
}
Question2:
{
printf("\
You have %d points", main_count);
printf("\
What letter am I thinking of now?");
scanf("%c", &q2_input);
switch (q2_input)
{
case 'D': case 'd':
printf("\
\
Correct!");
main_count += 5;
goto End;
break;
default:
printf("\
Nein!");
break;
}
}
End:
{
printf("\
\
You ended up with %d points!", main_count);
}
}
</pre><hr></blockquote>
Every time, you answer the first question properly, it sends you to question 2 and then directly to the default: statement.
Answering the first question incorrectly displays the default: statement twice before dropping you back to the first question.
Any help would be immensely appreciated.I'm new at this, so please forgive my ignorance and mistakes.
[code]
#include <stdio.h>
int main()
{
char q1_input = 0;
char q2_input = 0;
short main_count = 0;
printf("This is a very simple letter-guessing quiz");
Question1:
{
printf("\
\
What letter am I thinking of?");
scanf("%c", &q1_input);
switch (q1_input)
{
case 'C': case 'c':
printf("\
Excellent! You guessed right!");
main_count += 5;
goto Question2;
break;
default:
printf("\
Sorry, try again!");
goto Question1;
break;
}
}
Question2:
{
printf("\
You have %d points", main_count);
printf("\
What letter am I thinking of now?");
scanf("%c", &q2_input);
switch (q2_input)
{
case 'D': case 'd':
printf("\
\
Correct!");
main_count += 5;
goto End;
break;
default:
printf("\
Nein!");
break;
}
}
End:
{
printf("\
\
You ended up with %d points!", main_count);
}
}
</pre><hr></blockquote>
Comments
The source of your problem is likely to be something to do with how scanf works. If I recall correctly scanf doesn't return until you end the input line, and then it will read the key you typed plus the end-of-line character(s). scanf("%c\
", &input); might work better, but I don't have that much recent experience with console input in C.
", &q1_input); produced some interesting results. I tried swapping the position of %c and \
and made it work.
Thanks for your help! That little tidbit of info you gave me is nowhere to be found in the book I've been reading.
look at the to_upper function in stdio.h
[ 11-03-2002: Message edited by: Rick1138 ]</p>
Beginning C by Ivor Horton.
It only gets a breif mention, not exactly a reccomendation but it doesn't offer any alternatives.
I need to learn some.
Why all the hate for goto, anyway?
[ 11-04-2002: Message edited by: Rick1138 ]</p>