infinite loop

I am having issues with the code bellow. A programmer pointed out to me that I am going into an infinite loop if someone enters and invalid character. I have a portion of the code bellow but I have done the same thing in multiple places. The man also said I could remove it and incorporate it into the default of the switch statement....but how do I do this?

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
int main()
{
       int value;
       do {
              do {
                     cout <<"Would you like to convert the weight or length:\n"
                              "Press 1 for Lengths\n"
                              "Press 2 for Weight or\n"
                              "Press 0 to terminate program\n";
                     cin >> value;
                     if (value != 0 && value != 1 && value != 2)
                           cout << "Invalid entry." << endl;
                     } while (value != 0 && value != 1 && value != 2); 
              switch (value)
              {
                     case 0: 
							
                           break;
                     case 1: convert_lengths(); 
                           break;
                     case 2: convert_weights();
                           break;
              } 
       } while(value != 0);

       return 0;
}
Think about when the control will exit this loop

1
2
3
                     if (value != 0 && value != 1 && value != 2)
                           cout << "Invalid entry." << endl;
                     } while (value != 0 && value != 1 && value != 2); 


Oh I am sorry. You did not properly formatted the code that I interpretated it incorrectly.

I think that your prohtammer meant that instead of a number some letter can be entered. In this case the state of the input stream std::cin will be not active.
Last edited on
How do I do that? I am a beginner at C++ and there for only know bigger language but thank you for your help....just please show me the code you want me to enter!
The simplest way is to assign 0 to value inside the external loop

1
2
3
       do {
              value = 0;
              do {


Then in case of invalid input value will be equal to acceptable value 0. Or you should check the state of the straem and clear it if an error occurs.
but would that help me if the put the number 3 in for example into the input? which isn't a valid input for my program
What is the problem?

Invalid input means that instead of a number some other characters will be entered. 3 is a number and a valid input.
Last edited on
okay wait your right my fault.....if the user inputs any other value what will happen to the program?
If you enter anything that can't be resolved to an integer type, you're going to put cin into a fail state and you need to reset its flags. That's what cin.clear() is for.
Instead of this:
1
2
if (value != 0 && value != 1 && value != 2)
                           cout << "Invalid entry." << endl;

try this:
1
2
3
4
5
6
7
8
if (value != 0 && value != 1 && value != 2)
            {
                cout << "Invalid entry." << endl;
                //clear cin's flags
                cin.clear();
                //remove everything in the buffer up to and including the first newline
                cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }

You may have to #include <limits> .

Vlad's alternative of setting value = 0; will mean that any time cin can't coerce your input into an integer that value will remain zero, and you will get the behavior associated with the user putting in 0. In your case, the program would exit gracefully if the user inputs garbage.
Last edited on
Instead of all those do loops & code to check for invalid input, have default: clause in the switch, and have a quit case as well (your option 0 does that). The quit case should set a bool variable which ends the enclosing while loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool Quit = false;

while (!Quit) {
      //get input

     switch (MenuOption) {
           case 0: 
	       Quit = true;	 // execution continues after the while loop
                // could use return 0; here	or the exit function to end the program	
               break;
           case 1: 
                convert_lengths(); 
                break;
           case 2: 
                 convert_weights();
                 break;

           default:
                 std::cout << "Bad Input, Enter 0, 1 or 2"
                 break;

     } // end of switch
} // end of while 


I personally avoid do loops (I can mostly find a way to write them as while or for), and I really hate dislike constructs like these, No I really do hate them 8+) :

if (value != 0 && value != 1 && value != 2)

Also remember that break only breaks out of the inner most loop.

Hope this helps.

Last edited on
Wish it will be helpful

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
int main()
{
       int value;
       do {
                cout <<"Would you like to convert the weight or length:\n"
                "Press 1 for Lengths\n"
                "Press 2 for Weight or\n"
                "Press 0 to terminate program\n";

               cin >> value;

              switch (value)
              {
                     case 0: 
							
                           break;
                     case 1: convert_lengths(); 
                           break;
                     case 2: convert_weights();
                           break;
                     default:
                           if (value != 0 && value != 1 && value != 2)
                            cout << "Invalid entry." << endl;
                           break;

              } 
       } while(value != 0);

       return 0;
} 
Last edited on
Registered users can post here. Sign in or register to post.