I have a simple code: If ch
is Y
, then the first if
statement runs, if N
, then the other, if anything else, then the else
statement. But if I give more than one character, then it writes "wrong input" out many times. How can I solve this issue, so anything I write the else
statement only runs once?
#include <iostream>
using namespace std;
int main()
{
bool running = true;
char ch;
do{
cout << "Enter Y or N: ";
cin >> ch;
cout << endl;
if(ch == 'Y'){
cout << "You entered yes." << endl;
running = false;
} else if(ch == 'N'){
cout << "You entered no." << endl;
running = false;
} else{
cout << "Wrong input." << endl;
}
}while(running);
return 0;
}