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
@republic (1)

Wish it will be helpful


Not at all. Your post was 30 minutes after mine & just confuses the issue.

Are you trolling?
@TheIdeasMan


Pls. do not change the original code style by yours!

"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?
"






Three points to be annouced:

First:

If you want to modify the other's work, Pls. keep the smallest changes.

Second:

Why do I confuse the others? Pls. point it out.

Third:

What does the "trolling" mean?
Last edited on

If you want to modify the other's work, Pls. keep the smallest changes.


How is your code any different? It is almost identical to the one by TheIdeasMan.
BTW, you have a useless if condition on line 22.

@TheIdeasMan
Why do you hate do/while blocks?
@ Daleth

I change the original question not the TheIdeasMan's version.

BTW, I don't copy TheIdeasMan's code. It is naive.

You are extremely insinuating!

I change the original question...

Please elaborate how your post did so and was not code written in a style different from the OP's style.

I did not mean to imply that you were copying TheIdeasMan's code, sorry about that. I simply was pointing out that you were doing what you accuse TheIdeasMan of doing.
@republic

Trolling is what you seem to be doing now - being annoying & unhelpful & provoking comment. Like saying my code is naive when in fact it is a common solution to this problem.

Keep it up & I will report you.

@Daleth (219)

When I posted the solution, I didn't notice the former ones. Sorry about that.
@republic

So I reported your post, which removed it. If you don't want this to happen in the future - then be nicer. You were aggressive & provocative from the start.

Remember you run the risk of having your account restricted or closed by admin if he decides that your behaviour isn't suitable.

Topic archived. No new replies allowed.