Hi, sorry if a similar post has been posted before but I could not find one.
I'm messing around with C++ and I'm trying to make a statement which lets the user choose to either quit or re run.
i.e. press y to re run or n to quit. if user presses anything else they get a message to renter the answer.
I can do it with numbers i.e. press 1 or 2 but not with letters!
The following code is what I made:
int main()
{char ans = 'y';
while (ans == 'Y' || ans == 'y')
{
cout << "Hello World\n";
cout << "Would you like to see that again - Y or N?\n";
cin >> ans;
while (ans != 'y' || ans != 'Y' || ans != 'n' || ans != 'N')
{cout << "please try again - Y or N\n";
cin >> ans;}
}
cout << "bye bye";
return EXIT_SUCCESS;
}
the problem is it will go into the error loop fine but then wont exit it if i press n or y.
Can any one shed some light on why it would be doing this?
Your || operators need to be && operators for the second loop.
Why: Because with the way you have it, it evaluates to true always. Why? Because if you put in 'y', then the condition ans != 'Y' will be true, and the whole expression will be true. You can't have ans be four things at once.