I'm asking the user if he/she wants a drink. The only answer the user can type is Y for yes and N for no then there comes the final dialogue. I made the answer a char: char answer; then added an if statement. If the user types Y or N, everything's fine. However, whenever the user types Y(+ any letter) such as YHTY, the program still accepts it (same goes for N). I just need a single letter answer Y or N and other answer beyond that would lead to the else dialogue.
This is the code:
#include <iostream>
using namespace std;
int main()
{
char answer;
cout << "Do you want a drink? " << endl;
cout << "Y/N: ";
cin >> answer;
if(answer == 'Y' || answer == 'y'){
cout << "Okay. I'll bring you some." << endl;
} else if(answer == 'N' || answer == 'n'){
cout << "Okay suit yourself." << endl;
} else{
cout << "Please type just Y/N." << endl;
}
return 0;
}