Infinite Loop Bug

When I enter a character or other symbol into this section of my code, it infinitely loops.

When I put a number value into it, the error and re-input loop works just fine.

What I want to happen is that the code identifies an invalid input and requests a new input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  // Get the letter requested by the user
        cout << "\nChoose a number corresponding to a letter in SUZ to print.\n";
        cout << "1. S\n2. U\n3. Z\n";
        cout << "> ";
        cin >> letter;

        // Validate the input
        while (letter < MIN_CHOICE || letter > MAX_CHOICE)
        {
            // Explain the error
            cout << "\nSelect a designated number from the menu\n";
            cout << "1. S\n2. U\n3. Z\n";
            cout << "> ";
            cin >> letter;
            cout << "\n";
        }
http://sscce.org/
Now add the definitions of MIN_CHOICE etc, a main() and a couple of includes to make what you posted a self contained compilable example.

Otherwise, we just guess that you got MIN and MAX the wrong way round or something.
Hello PacificAtlantic,

In addition to what salem c has said I will point out this:

Line 1 says to choose a letter.

Line 5 suggests that based on the variable "letter" that it is expecting a character and not a number.

There is no indication as to how "letter" is defined, but if it is defined as a "chat" then the while condition is totally wrong.

Not knowing what to do I came up with a program that outputs this:

Choose a number corresponding to a letter in SUZ to print.
 1. S
 2. U
 3. Z
 4. Exit
 Enter choice > a

     Invalid entry! Must be a number.

Choose a number corresponding to a letter in SUZ to print.
 1. S
 2. U
 3. Z
 4. Exit
 Enter choice > 0

     Invalid choice! Try again.

Choose a number corresponding to a letter in SUZ to print.
 1. S
 2. U
 3. Z
 4. Exit
 Enter choice > 5

     Invalid choice! Try again.

Choose a number corresponding to a letter in SUZ to print.
 1. S
 2. U
 3. Z
 4. Exit
 Enter choice > 2

  After the while loop

 The number is: 2
 The letter is: U

 Press Enter to continue:


Once you provide better information a response can be better formatted to fit your problem.

Andy
If you try to extract a number from cin and the next character isn't a digit (or minus sign) the, stream goes into the fail state. After that, all extractions will fail until you clear the error.

Input validation is a perfect example of when it makes sense to break out of the middle of a loop.
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
34
35
36
37
#include <iostream>
using namespace std;

int main()
{    
    int letter;
    const int MIN_CHOICE=1, MAX_CHOICE=3;
    
  // Get the letter requested by the user
    while (true) {
        cout << "\nChoose a number corresponding to a letter in SUZ to print.\n";
        cout << "1. S\n2. U\n3. Z\n";
        cout << "> ";
        cin >> letter;
	if (letter >= MIN_CHOICE && letter <= MAX_CHOICE) {
	    // It's good
	    break;
	}
	
	if (cin.bad() || cin.eof()) {
	    // Some error other than invalid input
	    cout << "Error reading input. Good bye.\n";
	    return 1;
	} else if (cin.fail()) {
	    // Invalid input
	    cout << "That isn't a number\n";
	    cin.clear();
	    cin.ignore(1000000, '\n'); // ignore through end of line
	} else if (letter < MIN_CHOICE || letter > MAX_CHOICE) {
	    // Invalid number
            cout << "\nSelect a designated number from the menu\n";
	} else {
	    cout << "Unknown error reading input. Good bye.\n";
	    return 1;
	}
    }
}

Topic archived. No new replies allowed.