I am an inexperienced programmer making a program in C which uses switch statements that respond when the user inputs specific letters such as 'A', 'B' or 'C'. I also have a default case that outputs "That's not an option, try again." It does work fine, but it prints this message for every individual character inputted. So if the user inputs "No thanks" it will say the message 8 times!
Here are some excerpts from my code which I think are important:
scanf(" %c", &PI1);
default:
printf("That's not an option. Try again.\n");
break;
It tried turning PI1 into a string limited to 1 letter (I hoped it would help) but that made the switch statement stop working because the value was not an integer.
Edit: More code
while (Scene1 == 0) {
scanf(" %c", &PI1);
switch(PI1) {
case 'A':
if (Scene1War == 1) {
printf("The Goblin is cowed, and agrees to let you through if you spare his life.");
Scene1++;
} else {
printf("The Goblin laughs and states that if he wanted anything he'd take it from your corpse.\n");
}
break;
case 'B':
printf("You make a terrified expression and stare intently into the doorway behind the Goblin. \nHe is confused and turns around for a second.\n");
Scene1Dis++;
break;
case 'C':
if (Scene1Dis == 1) {
printf("You catch the Goblin by surprise and manage to gain the upper hand. He does manage to get away alive, however.");
Scene1++;
} else {
printf("You and the Goblin fight viciously. In the end he is killed and you have lost 1 HP.");
GobLife++;
HP--;
Scene1++;
}
break;
case 'D':
if (Class == 2) {
printf("You stand menacingly and tell the Goblin you mean business.\n");
Scene1War++;
} else if (Class == 3) {
if (Scene1Dis == 1) {
printf("You successfully sneak by the Goblin.");
Scene1++;
} else {
printf("The Goblin notices you trying to sneak past, and stops you.\n");
}
} else {
printf("That's not an option. Try again.\n");
}
break;
default:
printf("That's not an option. Try again.\n");
break;
}}
Please don't make fun of my silly Goblin game thing. It's just a project to practice.