Timer

I'm supposed to create a program that inputs a start time and end time in the format hr:min:sec and then finds the total time elapsed. Also, it's using a 24-hour clock. I haven't completely written my code yet but I'm stuck on my loop. I'm trying to make sure that the user enters valid times (less than 60).

I realize I'm not finished yet but I'd like to fix this before moving on. Any ideas?

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
#include <iostream>
using namespace std;
int main ()
{
	
	int shrs, smins, ssecs;
	int fhrs, fmins, fsecs;
	char junk;
	bool isValid = true;
	
	//get times
	cout << "Enter the starting time." << endl;
	cin >> shrs >> junk >> smins >> junk >> ssecs;
	
	//check times
	while (cin >> shrs >> junk >> smins >> junk >> ssecs)
	{
		if (shrs > 60 || smins > 60 || ssecs > 60)
		{
			cout << "Invalid starting time, please re-enter." << endl;
		}
		else
		{
			cout << "Enter the time of finish." << endl;
			cin >> fhrs >> junk >> fmins >> junk >> fsecs;
				
		}
		if (fhrs > 60 || fmins > 60 || fsecs >60)
		{
			cout << "Invalid starting time, please re-enter." << endl;
		}

	}
	cout << "Starting time: " <<shrs << junk << smins << junk << ssecs << endl;
	cout << "Ending time: " <<fhrs << junk << fmins << junk << fsecs << endl;
		
}
Can you not use functions? It would simplify things a lot if you wrote a function that prompts the user for the input, validates it, and returns only when the user has entered valid data.
Topic archived. No new replies allowed.