I'm having trouble with a switch/case statement I created to work as a character check on an input.
My understanding of the switch/case statement is the following
switch (variable)
{
case value0:
Code to execute if variable==value0
break;
case value1:
Code to execute if variable==value1
break;
...
default:
Code to execute if variable does not equal the value following any of the cases
break; // not necessarily needed but good practice
}
So what I came up with for character validation was the following: (sorry the formatting isn't great! validInput is set to 0 before this, I cut some code out to help highlight my issue)
while (validInput==0) // creates loop awaiting valid input
{
scanf("%c",&replay);
switch (replay) // BUG TO FIX, displays default text before character entry
{
case 'Y':
case 'y':
//printf("Yes key pressed\n"); //debug
turns = lives;
validInput = 1;
break;
case 'N':
case 'n':
//printf("No key pressed\n"); //debug
end = yes;
validInput = 1;
break;
default:
printf("Invalid input, please try again\n");
break;
}
}
What I get each time is
Invalid input, please try again
*Then HERE I can enter the character and the behaviour is as expected. Y (or y) restarts like I want it to and N (or n) exits the game. All other keys show the text twice, I have shown my understanding of the order below:
Invalid input, please try again
l
Invalid input, please try again
Invalid input, please try again
R
Invalid input, please try again <- this is the error message I want to display
Invalid input, please try again <- this is the 'awaiting input' stage...
etc.
Why is it showing the default case at the beginning when it's awaiting the input? I really don't fancy writing an extra long case for all other keys!
EDIT I thought this was a switch/case issue but turns out it was a scanf issue. I didn't know that but thank you to those who helped fix it. My question was given -1 because of this :(