while loop and failbit with other conditions unexpected behavior

Why doesn't this work?

1
2
3
4
5
6
while (!(std::cin >> stat) || (stat != "hp" || stat != "str" || stat != "aff"))
{
	std::cin.clear();
	std::cin.ignore();
	std::cout << "Invalid Entry.\n";
}


But this does?
1
2
3
4
5
6
while (!(std::cin >> stat) || stat!= "hp")
{
	std::cin.clear();
	std::cin.ignore();
	std::cout << "Invalid Entry.\n";
}


I am trying to stop a failbit but at the same time stop the user from entering none related strings. The only string I wish to allow are "hp", "str" or "aff". Why does it work with one string but not other options?

I realize I could do this with an if statement but I'd rather use while loops.
If stat != "hp" is false then you can be sure stat != "str" will be true because it can't be equal to "hp" and "str" at the same time. Since you use || it's enough that one of them is true for the whole expression to be true, and that is always the case here. I think you should probably use && instead of || when testing the different string values because you want the loop to run once more if ALL of them is true, not just if ONE of them is true.
Last edited on
Wow.
This post is probably a reminder that I should not be doing this at 3AM.
Time to go to bed, good explanation Peter thanks a lot for your help.

Topic archived. No new replies allowed.