Bit of an odd request but can someone show me an example of a while loop in which the user gets asked just before for an integer input and if it isn't correct it will enter the while loop and loop until the right input is given... I just can't seem to do it :S Here's what I've done:
1 2 3 4 5 6 7 8 9 10 11
std::cout << "\nHow do you wish to fight: ";
std::cin >> fightType;
std::cin.ignore();
while(!std::cin && (fightType!=1 || fightType!=2)) { // wrong, change code <<<<<<
std::cout << "--Invalid input--\n\n";
std::cout << "How do you wish to fight: ";
std::cin.clear();
std::cin.sync();
std::cin >> fightType;
std::cin.ignore();
}
If you enter a letter then it comes up with invalid input, so that works fine, but when you enter a number other than 1 or 2 it still works
Let's see if I can re-explain this and hopefully lead you to an answer. :)
As it stands, lets consider the different possibilities.
fightType is 1:
Is it not equal to 1? Nope, it is 1.
Since we have an or, maybe the other condition is true; if that's so we can enter the loop.
Is it not equal to 2? Aha, it's 1, which isn't 2.
So we enter the loop.
fightType is 2:
Is it not equal to 1? Yep, it's a 2.
So we enter the loop.
fightType is 3,4,0, etc:
Is it not equal to 1? Yes.
We enter the loop.