Infinite do-while loop

Been helped on this forum before with suggestions and ideas for assignments in my beginning C++ class. I used suggestions and got the response I was looking for however, the month associated with it's switch case number executes but it is an infinite loop. How do I prevent this infinite loop?

Here is the code in it's entirety. The 2nd half of the (switch cases) is where my problem lies. Not too sure my do-while loop is correct, either. Any direction would be greatly 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>

using namespace std;

int main()
{
	int num1, num2;
	int monthNum = 0;
	
	cout << "This program will determine which number is larger than or 
                 smaller than between the two numbers you choose. \n\n";
	cout << "Pick two numbers between 0-100: ";
	cin >> num1 >> num2; 
	cin.get();
		
		if (num1 > num2)
		{
			cout <<num1<< " is larger than "<<num2<< endl;
			cout <<"Your first variable is the biggest." <<endl;
		}
		else if (num1 < num2)
		{		
			cout <<num1<< " is less than "<<num2<< endl;
			cout <<"Your second variable is the biggest." <<endl;
		}
		else 
		{
			cout <<num1<< " is equal to "<<num2<< endl;
		}

system ("PAUSE");

        
		cout <<"\n\n\n";
		cout <<"This portion of the program determine the month based 
                        on your numerical input." <<endl;
    	cout <<"Please pick a number between 1-12: ";
		cin >> monthNum;
		cin.get();

	do
	{
		 switch(monthNum)
			{
			case 1:
				cout <<"January" <<endl;			
				break;
			case 2:
				cout <<"Feburary" <<endl;
				break;
			case 3:
				cout <<"March" <<endl;
				break;
			case 4:
				cout <<"April" <<endl;
				break;
			case 5:
				cout <<"May" <<endl;			
				break;
			case 6:
				cout <<"June" <<endl;
				break;
			case 7:
				cout <<"July" <<endl;
				break;
			case 8:
				cout <<"August" <<endl;
				break;
			case 9:
				cout <<"September" <<endl;			
				break;
			case 10:
				cout <<"October" <<endl;
				break;
			case 11:
				cout <<"November" <<endl;
				break;
			case 12:
				cout <<"December" <<endl;
				break;
			default:
				cout <<"You did not pick a number between 
                                        1-12." <<endl;
				cout <<"Please try again and choose a number 
                                        between 1-12: ";
				cin >> monthNum;
				cin.get();
		     }	
	} while (monthNum > 0);

cin.get();
return 0;
}
closed account (jwC5fSEw)
Nowhere in the do-while loop do you ever change monthNum, assuming you start with a number between 1 and 12. What this means is monthNum will ALWAYS be greater than 0, so your loop will constantly execute because the condition is always true.
So I shouldn't use a 'do-while' to redirect the flow back to the part asking for the user to re-enter a number? I need to find a way for redirection back to the user input part and to display the correct month associated with its month number. The only place I can change monthNum is in the actual while clause. I tried and it doesn't work like I thought it might've.

Can you clarify? I don't mean to sound like a noob, but I am....in C++. So, forgive my questions if I can't quite grasp how to correct my infinite loop.

Thanks all.
closed account (jwC5fSEw)
Don't worry, everybody was a beginner once. I still I am myself; it was only about a month ago that I was starting.

You have the right idea in using a while loop, but the problem is that you haven't provided a way out of the loop. The way to do this would be to have the while loop check a bool to see if it's true. In the loop, get the user input. Then, if the input is valid (i.e. in the range you want), set the bool to true so the while loop will stop. If the input is invalid, the loop will repeat and the user will be prompted for input again.

Also, on a more minor note; instead of using a do-while loop, you could just use a while loop and put the user input part at the beginning. Then, lines 37-39 would not be repeated in lines 84-86.
Shadow,

Thanks man! You got me up and running. I did as you suggested in your "minor note" and all is well now.

Thanks again!
closed account (jwC5fSEw)
No problem! I'd be stuck on the bare basics if not for this forums, and I'm glad to give back to it any way I can.
Topic archived. No new replies allowed.