I cannot for the life of me figure out how to make this piece of code not accept a character as well as numbers < 1 and numbers > 5. Everything I have tried does an infinite loop
with this configuration of this piece of code, I am trying to make it to where if the user enters anything besides the correct selections, then the variable characters is set equal to -1. Then the while loop tests while character = -1. Logically this should work. I have used breakpoints and cout statements to see what the value of characters is holding at those specific points where it assigns -1 and it does hold -1. So why is my loop infinite still when I put in a character, when I have tested to see what value character holds after entering a character and it does hold -1...
im confused. Ive also tried cin.fail() but could not get it to work.
The cout's were there for testing purposes. you can delete them if annoying.
cout << "Please choose the character you would like to play\n\n";
// Display character selection menu.
characterSelection();
// Choose your Character
cin >> character;
cout << character;
if (character != 1 && character != 2 && character != 3 && character != 4 && character != 5)
{
character = -1;
}
cout << character;
while (character == -1)
{
cout << "Please choose a correct selection...\n\n";
cin >> character;
if (character != 1 && character != 2 && character != 3 && character != 4 && character != 5)
{
character = -1;
}
cout << character;
}
The switch control structure can be used with constant chars or ints, sounds as though you are after the char version. switch is great for this, make sure you have a default case to catch bad input.
I do not see how it does not change from the invalid flag. it is while character = -1. If i enter 1 or 2 or 3 or 4 or 5, then those are valid and it continues on. If I enter anything less than 1 or greater than 5, then it asks me to input again and does not spiral into infinite loop. but if I enter a character, bam. even though the character value itself is like -81929873249829 or whatever, I am assigning it to -1 when it is "invalid" and testing if character = -1, which it should and then ask me again.
The switch statement would solve this but I a really curious as to why this itself is not working. this is the most odd thing I have encountered in c++ so far.
it uses that if statement twice. The first to check if it was any of the choices and if not character = -1 before the loop. In the loop, it does the same thing to make sure that if the user entered another invalid choice, that character = -1 again to continue the loop.
it works if a valid choice was entered first, it continues on. if an invalid integer is entered it stops and asks, then if valid choice is entered it continues on.
Also I have noticed that if I enter a character, it completely skips over the cin >> character statement that I have to allow the user to try again and just goes infinite. HOW??????
it is an int. I think I may have learned that if I am trying to enter a char into a declared int variable, then no value is given. Im not completely sure if this is correct.
When you enter a character when cin is expecting an int you will cause cin to fail. You can use cin.clear() to get rid of the failbit on cin and then use cin.ignore(numeric_limits<streamsize>::max(),'\n'); to ignore the input that caused cin to fail in the first place. Try working this into your program and see if that resolves your problems.
1 2 3 4 5
if(cin.fail()) //if the user enters bad input
{
cin.clear(); // Clear the failbit from cin
cin.ignore(numeric_limits<streamsize>::max(),'\n'); //Ignore everything on cin until the next newline
}
If you want to use numeric_limits<streamsize>::max() then you must #include <limits>, but the first argument for cin.ignore() is just the number of characters to ignore. You could just as easily replace it with 100 or another number. numeric_limits<streamsize>::max() is nice because it gives the max size cin can hold.