clock - very incomplete, but trying to understand classes

Im working on this clock project and im trying to set it up so it checks if hr is greater than 12 for am/pm. I keep getting the error 'class Time' has no member named 'hr' I thought the hr would go to void settime and be the value for hour?

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>
#include <string>
#include <fstream>

/*Modify the example program (below) that implemented the Class Time to:
1) Create the UML notation for the Class Time. Submit it as doc or text file.
2) Create a menu as follow:
Press #1 to enter hours [1-12] with PM and AM, minutes and seconds.
Press #2 to display the time in military mode.
Press #3 to display the time in standard mode.
Press #4 to exit.
Any other entry different than 1, 2, 3 or 4 will be rejected and will display the whole menu again. */

using namespace std;

class Time{
	public:
		time();
		void settime(int, int, int);
		void gettime();
		
	private:
		int hour;
		int minute;
		int second;
		
};

Time::time()
{
	hour = 0;
	minute = 0;
	second = 0;
	
}
void Time::settime(int hr, int m, int s)
{
	hour = hr;
	minute = m;
	second = s;
}

void Time::gettime()
{
	cout << "hours: " << hour <<" "<< "minute: " << minute <<" "<< "second: " << second <<" " << endl;
}
int main()
{
	int num;
	Time time;
	char mode;
	

	do
	{
		cout<<"Press #1 to enter hours [1-24], minutes and seconds.\n";
		cout<<"Press #2 to display the time in military mode.\n";
		cout<<"Press #3 to display the time in standard mode.\n";
		cout<<"Press #4 to exit.\n";
	
	cin>>num;
	
	if (num==1)
	{
		int hr,m,s;
		
		
		cout<<"Enter hours [1-24], minutes and seconds.\n";
		cin >> hr >> m >> s ;
		if (time.hr>12)
		{
			time.hr=time.hr-12;
			cout<<Time.settime(hr,m,s);
		}
			
		
		
		{
			
		}
	if (num==2)
	{
		
		if (h>12)
				{
					(mode==military)
					cout<<"military time.\n";
				}				
	}
	if (num==3)
	}
	}
}

Last edited on
On line 18, time(); , is that your default constructor?

If the answer is yes, then, line 29 Time::time() should look like this, Time::Time() and line 18 should look like Time();.
so like 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
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
94
95
96
#include <iostream>
#include <string>
#include <fstream>

/*Modify the example program (below) that implemented the Class Time to:
1) Create the UML notation for the Class Time. Submit it as doc or text file.
2) Create a menu as follow:
Press #1 to enter hours [1-12] with PM and AM, minutes and seconds.
Press #2 to display the time in military mode.
Press #3 to display the time in standard mode.
Press #4 to exit.
Any other entry different than 1, 2, 3 or 4 will be rejected and will display the whole menu again. */

using namespace std;

class Time
{
	public:
		Time();
			void settime(int, int, int);
			void gettime();
	
	private:
		int hour;
		int minute;
		int second;
		
};

Time::Time()
{
	hour = 0;
	minute = 0;
	second = 0;
	
}
void Time::settime(int hr, int m, int s)
{
	hour = hr;
	minute = m;
	second = s;
}

void Time::gettime()
{
	cout << "hours: " << hour <<" "<< "minute: " << minute <<" "<< "second: " << second <<" " << endl;
}

int main()
{
	int num;
	
	char mode;
	

	do
	{
		cout<<"Press #1 to enter hours [1-24], minutes and seconds.\n";
		cout<<"Press #2 to display the time in military mode.\n";
		cout<<"Press #3 to display the time in standard mode.\n";
		cout<<"Press #4 to exit.\n";
	
	cin>>num;
	
	if (num==1)
	{
		int hr,m,s;
		
		
		cout<<"Enter hours [1-24], minutes and seconds.\n";
		cin >> hr >> m >> s ;
		if (Time.hr>12)
		{
			Time.hr=Time.hr-12;
			cout<<Time.settime(hr,m,s);
		}
			
		
		
		{
			
		}
	if (num==2)
	{
		
		if (hr>12){
			(mode=military)
				cout<<"Military time: "<< hr << m << s ;
			}
		else
								
	}
	if (num==3)
	}
	}
}
Last edited on
Line 72 and 74: Class Time has no member named hr, but rather hour. Hour is declared to be private, so you have no access to it from outside of its class. You could solve this by declaring a public getHour() method or making hour public.
Thanks for that @nuderobmonkey I figured it out with your help. I changed it up a bit to add a while loop. I dont know what exactly I'm doing wrong here but it returns to the main menu before going to the 'if' statement thats included. I looked back at some past work i have done and it was setup exactly like this except there were no classes involved. I'm just trying to return to the main menu after performing the action of the selection.

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <string>
#include <fstream>

/*Modify the example program (below) that implemented the Class Time to:
1) Create the UML notation for the Class Time. Submit it as doc or text file.
2) Create a menu as follow:
Press #1 to enter hours [1-12] with PM and AM, minutes and seconds.
Press #2 to display the time in military mode.
Press #3 to display the time in standard mode.
Press #4 to exit.
Any other entry different than 1, 2, 3 or 4 will be rejected and will display the whole menu again. */

using namespace std;

class Time
{
	public:
		Time();
			void settime(float hr, float m, float s);
			void gettime(float hr,float m,float s);
			void setmidday(char am, char pm);
			void setmode(char standard, char military);
	private:
		float hour;
		float minute;
		float second;
		
};
void Time::setmidday(char am, char pm)
{
	pm=(hour>=12);
	am=(hour<12);
}
void Time::setmode(char standard, char military)
{
	standard=12;
	military=24;
}

Time::Time()
{
	hour = 00;
	minute = 00;
	second = 00;
	
}
void Time::settime(float hr, float m, float s)
{
	hour = hr;
	minute = m;
	second = s;
}

void Time::gettime(float hr,float m, float s)
{
	cout << "hours: " << hour <<" "<< "minute: " << minute <<" "<< "second: " << second <<" " << endl;
}



int main()
{
	
	int num, ok=0;
	Time t;
	int hr,m,s;

	
while(true)
	
	{
	
		cout<<"Press #1 to enter hours [1-24], minutes and seconds.\n";
		cout<<"Press #2 to display the time in military mode.\n";
		cout<<"Press #3 to display the time in standard mode.\n";
		cout<<"Press #4 to exit.\n";
		
		cin>>num;
		
		
if (num == 1){
		t.settime(hr, m, s);
	}else if (num == 2){
		t.gettime(hr,m,s);
	}else if (num == 3){
		t.gettime(hr,m,s);
	}else if (num == 4){
		{
			cout<<"bye!bye!\n";
  			exit;
  			break;
  		}
	}
}
}
void settime(float hr, float m, float s)
{
	int ok=0;
	do
	
	{
		
		{
			cout<<"Enter hours [1-24], minutes and seconds.\n";
			cin >> hr >> m >> s ;
	
		}while (ok==0);
		if (hr>=12)
		{
			
			(hr=hr-12);
			cout << hr <<":"<< m << s <<" "<< "pm\n";
			ok=1;
		}	
		else if (hr<12)
		{
			cout << hr << ":" << m << s << " " << "am\n";
			ok=1;
		}
			
		}while (ok==0);	
}
void gettime(float hr,float m,float s)
{
	int ok=0, num1;
	do	
	{
		cout << "Enter '24' for Military time or '12' for Standard time.\n";
		cin >> num1;
	}while (ok==0);
	do
	if (num1==24)
		{
			cout<<"Military time: "<< hr <<":"<< m << s;
			ok=1;
			
		}while (ok==0);
	
	
	
	if (num1==12)
		{
			cout <<"standard time: "<< hr<<":"<< m << s;
			ok=1;
		
		}while (ok==0);
}
What I riginally wrote worked better in the short run but I changed because theres some problems with that that I couldnt figure how return to main menu without a loop. But now I dont get anything but the main menu. I dont know how to get that loop working correctly. it keeps going back to line 74 and I never get line 82 to work. idk if a loop has to be different with classes?
Topic archived. No new replies allowed.