Validating Status Code Input

I'm having difficulties figuring out this block of code.
I want to have the user input A, S, or, Y as the status code, and if incorrect
to loop and display an error message until it's correct. Once correct, I want it to display a message stating it's valid, and from there moving on. Here's what I have.

1
2
3
4
5
6
7
 do
 {
 	cout << "Status Code Invalid!" << endl;
 	cout << "Enter Status Code: ";
 	cin >> status_code;
 } while (status_code != 'A' || status_code != 'S' || status_code != 'Y');
You'll need to make use of an if statement at the beginning of your do loop. Otherwise, it will display the error message even before you've entered anything.
What would be the condition of the if statement?
Well, you want to display an error message when the input was neither A, S, or Y, right? So I'd say it would have something to do with that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
 do
 {
 	cout << "Enter Status Code: " << endl; 
 	cin >> status_code;
 	
 	if (status_code == 'A' || status_code == 'S' || status_code == 'Y')
 	{
 		cout << "Valid Status Code!" << endl;
 	}
 	
 	else 
 	{
 		cout << "Invalid Status Code!" << endl; 
 	}
 } while (status_code != 'A' || status_code != 'S' || status_code != 'Y');


I tried doing this, I don't really understand how I could put this within an if statement.
closed account (48T7M4Gy)
Here is a starting point. You can extend it by using toupper for case conversion and including as many tests as you want (Y etc)

Note that if multiple characters are typed in problems occur but that is an embellishment you can add to via cin.ignore() etc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    char status_code;
    char prompt[] = "Enter code: ";
    
    std::cout << prompt;
    
    while( std::cin >> status_code and status_code != 'A' and status_code != 'S')
    {
        std::cout << "Error\n";
        std::cout << prompt;
    }
    
    return 0;
}
Topic archived. No new replies allowed.