Recognizing whitespace entry in a simple menu

I created a simple menu. During validation it occurred to me that if a user enters a space or 'enter' as an option, it does nothing. How can I make the program respond as it does when an invalid character is entered? I can't find any help on this. Below is the code I have so far. Thanks in advance!!


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
  int num;
    
    cout << setw(30) << "QUIZ RESULTS MENU     " << endl;
    cout << setw(30) << "***************************" << endl << endl;
    
    cout << "   1. " << left << setw(30) << "List by Student ID" << endl;
    cout << "   2. " << setw(30) << "List by Student ID and Grades" << endl;
    cout << "   3. " << setw(30) << "List by Letter Grade" << endl;
    cout << "   4. " << setw(30) << "List by Numeric Grade" << endl;
    cout << "   5. " << setw(30) << "List Grades by Percentage" << endl << endl;
    
    cout << right << "Enter an option: " ;
    cin >> num;
    cout << endl;
       
    if (num == 1)
       cout << "This option sorts the student list by ID and writes it to a file." << endl << endl;
    if (num == 2)
       cout << "This option searches by student ID and displays the ID, letter grade and numeric grade." << endl << endl;
    if (num == 3)
       cout << "This option searches for students with a particular letter grade." << endl << endl;
    if (num == 4)
       cout << "This option sorts the list by numeric grade." << endl << endl;
    if (num == 5)
       cout << "This option calculates the % of students with a particular grade." << endl << endl;    
    if (num < 1 || num > 5)
       cout << "This is not one of the menu options. Please try again." << endl << endl;
    [code]
[/code]
Last edited on
Firstly, edit your post so that it uses code tags - the <> button on the right.

I think you would be much better to use a switch instead of the if statements. Switch has a default label, which you can use to catch anything that doesn't match any other case.

Also, display of the menu should be a function, the processing of the response should be a function as well. You can call a function from each case in the switch, to carry out hat particular option.

Hope all goes well.
Thanks! Let me look into using switch. I just resubmitted with the code tags. Sorry about that.
By default cin >> num; ignores white spaces.
Here's the switch. Still works the same. If the user enters a <space> or <enter>, it does nothing until a character is entered. Can I use the <space> or <enter> as a case? Didn't seem to work with '\n'.

I tried also it with declaring char num; then cin.get(num); which will capture the <enter> and fire the default statement, but then i have issues with integers of more than one digit. Ex: case 3 is true for '345' reading only the first character this way.

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
int num;
    
    cout << setw(30) << "QUIZ RESULTS MENU     " << endl;
    cout << setw(30) << "***************************" << endl << endl;
    cout << "   1. " << left << setw(30) << "List by Student ID" << endl;
    cout << "   2. " << setw(30) << "List by Student ID and Grades" << endl;
    cout << "   3. " << setw(30) << "List by Letter Grade" << endl;
    cout << "   4. " << setw(30) << "List by Numeric Grade" << endl;
    cout << "   5. " << setw(30) << "List Grades by Percentage" << endl << endl;
    
    cout << right << "Enter an option: " ;
    cin >> num;
    cout << endl;
     
    switch(num)
    {   
        case 1: cout << "This option sorts the student list by ID and writes it to a file." << endl << endl;
                break;
        case 2: cout << "This option searches by student ID and displays the ID, letter grade and numeric grade." << endl << endl;
                break;
        case 3: cout << "This option searches for students with a particular letter grade." << endl << endl;
                break;
        case 4: cout << "This option sorts the list by numeric grade." << endl << endl;
                break;
        case 5: cout << "This option calculates the % of students with a particular grade." << endl << endl;
                break;
        default: cout << "This is not one of the menu options. Please try again." << endl << endl;
    }
You can use either a char or an int. With char you can only do single chars by definition.

When using char remember the single quotes '1'

So if your menu had 10 items, your would need an integral type like unsigned short.
Topic archived. No new replies allowed.