Why won't my program get out of the first function?

I finally got my program to compile but it won't leave the first function.


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
#include <iostream>
#include <iomanip>
using namespace std;

struct Time
{
	int hours;
	int minutes;
	int seconds;
};

void getTime(struct Time& time);
bool isTimeValid(struct Time& time);
void addOneSecond(struct Time& time);
void displayTime(struct Time& time);

const int MAX_HOURS = 23;
const int MAX_MINS = 59;
const int MAX_SECS = 59;

int main()
{
	Time time;

	char choice = 'Y';
	
	do
	{
		system("CLS");
		getTime(time);
		isTimeValid(time);
		addOneSecond(time);
		displayTime(time);
	} while (toupper(choice == 'Y'));

	return 0;
}
void getTime(Time& time)
{
	do {

		cout << "Enter the time in \"military time\", (24-hour format), in the following\norder : HH:MM:SS, (Hours, Minutes, Seconds).\nHours: ";
		cin >> time.hours;
		cout << "\nMinutes: ";
		cin >> time.minutes;
		cout << "\nSeconds: ";
		cin >> time.seconds;

	} while (isTimeValid == false);
}
bool isTimeValid(Time& time)
{
	if ((time.hours >= 0) && (time.hours <= MAX_HOURS) && (time.minutes >= 0) && (time.minutes <= MAX_MINS) && (time.seconds >= 0) && (time.seconds <= MAX_SECS))
	{
		return true;
	}
	else
	{ 
		return false;
		cout << "Invalid Time";
	}
}
void addOneSecond(Time& time)
{
	time.seconds++;

	if (time.seconds > MAX_SECS)
    {
	time.seconds = 0;
	time.minutes++;
    }

	if (time.minutes > MAX_MINS)
    {
	time.minutes = 0;
	time.hours++;
    }
	if (time.hours > MAX_HOURS)
	{
		time.hours = 0;
	}
}
void displayTime(Time& times)
{
	char choice = 'Y';
	cout << "\nAfter adding one second, the time is " << setw(2) << cout.fill('0') << time << ".";
	cout << "Do it again? (Y/N)";
	cin >> choice;
}
Line 49: isTimeValid is supposed to be a function taking a parameter of type Time. No idea why this program actually compiles, unless it thinks that is a function pointer or something.

Also, in your main routine, choice is never changed (it knows nothing about the completely separate variable choice in displayTime() ), so that while loop would go on for ever.

Line 83: times was presumably intended to be time.

If you are going to use system() then please include <cstdlib>.


BTW. time is part of the standard library - it is not a very good choice of variable name.
Last edited on
Topic archived. No new replies allowed.