How do I get validate inputs that requires a number or letter?

Hello everyone,
My program requires that I use a menu for the user to select an option. The options are 1,2,3, or D(Exit). My issue is that my function will recognize and validate numbers but not letters. For example, if I input a 4 it will loop until I get a 1,2,or 3; on the other hand, if I input a f, it will go through the loop infinitely without giving a prompt. Same goes for all letter including D. I tried changing choice to be a unsigned char but that messed up my options for displaying later on in the program, so it needs to be an int. Any help would be appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  //*********************************************************************************
int mainMenu()
{
	//variables
	int choice;
	
	cout << endl;
	cout << "**************************************************" << endl;
	cout << "\t\t\tMain Menu" << endl<< endl;
	cout << "\t1 - Change office from occupied to empty" << endl;
	cout << "\t2 - Modify office occupant type" << endl;
	cout << "\t3 - Change office from empty to occupied" << endl;
	cout << "\tD - Done making modifications" << endl << endl;
	cout << "***************************************************" << endl;
	
	cout << " Please enter menu choice (1, 2, 3, D): ";
	cin >> choice;
	choice = toupper(choice);
	cout << endl;
	
	if (choice == 'D')
		return 5;
	while (choice != 1 && choice != 2 && choice != 3 && choice != 'D')	
	{
		//cin.clear();
		cout << "ERROR! an invalid choice was entered. Please enter one of the following  "
			 << "choices (1, 2, 3, or D): ";
		cin >> choice;
		choice = toupper(choice);
	}
		
	return choice;
}
one approach to this is to read input to a string.
Then validate the string data.
Then change the string data into the proper input.


Random question.
Have you lived in San Jose before? Is your name Kevin?
Last edited on
Question: are you locked in to using numbers for menu choices and a character for the exit option? Aka if this is a school assignment or something similar for academic needs, are those the instructions you absolutely need to follow?

Because otherwise I would give your menu options E(mpty), M(odify), O(ccupied), D(one) and have your choice as a char type.
Last edited on
1
2
int choice;
cin >> choice;
Will read only numbers. Will not read characters at all, because you prohibited it by declaring your variable as int.

choice = toupper(choice); Meaningless. Does not do what you think it does.

Think about it: you do not need numbers 1, 2, etc., you want characters '1', '2','3', '4' and 'D'.
So you just need to declare choice as char.
I am locked into using 1,2,3, and D (exit). I tried to change the 1,2,3 into a char, which works for that particular function, but when I go to display it doesn't recognize the char, because its set up to see it as an int.

Are you talking about reading the input as a char and validating it, than changing it back to an int? I guess I'm not sure how to go about doing that.

My name is Joe; although, I think San Jose would be a cool place to live, I never lived there before.
Can I static_cast the 'D' into an int?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void menu_header()
{
	std::cout << "\n**************************************************\n"
	             "\t\t\tMain Menu\n\n"
	             "\t1 - Change office from occupied to empty\n"
	             "\t2 - Modify office occupant type\n"
	             "\t3 - Change office from empty to occupied\n"
	             "\tD - Done making modifications\n\n"
	             "***************************************************\n" ;
}

char get_choice()
{
    std::cout << "Please enter one of the following choices (1, 2, 3, D): " ;
    char choice ;
    std::cin >> choice ;
    return choice ;
}

int main_menu()
{
    menu_header() ;

    while( char choice = get_choice() ) // assume that a null character won't be entered
    {
        if( choice == 'D' || choice == 'd' ) return 5 ; // D

        else if( choice > '0' && choice < '4' ) return choice - '0' ; // 1-3 (note: '2' - '0' == 2 etc.)

        else std::cout << "ERROR! an invalid choice was entered\n\n" ; // bad input
    }
}
Last edited on
Thank you JLBorges, that cleared up everything.
Topic archived. No new replies allowed.