Why does this loop? (probably stupid simple--but I cant think it through)

Writing a program that simply converts time. Have the program written and it compiles just fine--however...

When the program asks for the user to enter the hours (1-12) and the user enters a valid hour, the program loops--just asks for the user to enter the hours again, which is obviously incorrect.

If the user enters a value < 0 or > 12 the program prints the correct exception and loops to ask the user to input the hours again (which is correct).

code for the hour entry is listed below--thank you for your help!

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
int hourEntry(int hours) 
{
	bool running=false;
         hours = 0;
	do
	    {
		try
		   {
			cout << "Please enter the hours (1-12): ";
			cin >> hours;
            cout << endl;
			if (hours > 12 || hours <0)
			{
			throw invalidHr();
			running = true;
                        }
		    }
		catch (invalidHr invHr)
		   {
	     		cout <<"Runtime Error: " <<invHr.what()<<endl;
		   }
         }
	while (!running);
	return hours;
}
Last edited on
Hello CapitalJeep,

Think about it. If the hour is correct where does "running" change to true? You might try an else running = true; to go with the if statement.

Hope that helps,

Andy
Andy,

Thank you!

Sometimes I just have to shake my head at myself (I seem to have a lot of problems thinking through loops--I probably need to start making flowcharts to assist me).

moving that line and adding the if statement worked wonders!

Topic archived. No new replies allowed.