check validity by using loop

I want to write a programme that the valid input is A,B,C,a,b or c.
if the user input other letters, the programme will require the user to input the letter again.

how can I simplify the programme?

#include<iostream>
using namespace std;
int main()
{
char letter;
cout << "input letter ";
cin >>letter;
while (letter!='A' && letter != 'B'&& letter != 'C'&& letter != 'a'&& letter != 'b'&& letter != 'c')
{
cout << "Invalid Input\n";
cout <<"input letter";
cin >> letter;
}
}
Use toupper or tolower with the letter so that you can halve the length of your while loop's condition?
http://www.cplusplus.com/reference/clibrary/cctype/tolower/

You can also merge your cout statements in the loop, like so:
cout << "Invalid Input\n" << "input letter";

-Albatross
Last edited on
Topic archived. No new replies allowed.