Declaring AM/PM as a string

So I have to make a clock in HH:MM:SS AM/PM format with the AM and PM being part of the string. Here is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  	ostream &operator<<(ostream &out, Clock &time){
		
		if(time.mHours >12)
			time.mHours = time.mHours - 12;
		else
			time.mHours = time.mHours;

		if(time.mHours>12)
			string display= "PM";
		else if(time.mHours<12)
			string display= "AM";

		out << time.mHours;
		out << ":"<< time.mMinutes <<":"
		<< time.mSeconds  <<string<< endl; 
		return out;

There is a lot more to the code but this is the part I need help with. I had it working when I had it as a cout and just displaying it, but I need to change them to be part of a string. I'm looking through my book but I am unsure as to how to do this.
Last edited on
@jkelly18

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ostream &operator<<(ostream &out, Clock &time){
		
		if(time.mHours >12)
			time.mHours = time.mHours - 12;
		else
			time.mHours = time.mHours;

		if(time.mHours>12)
			string display= "PM"; // Can not happen, since you already subtracted 12 if mHours was over 12
		else if(time.mHours<12) // display will be AM
			string display= "AM";

		out << time.mHours;
		out << ":"<< time.mMinutes <<":"
		<< time.mSeconds  <<string<< endl; 
		return out;


Try this way...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ostream &operator<<(ostream &out, Clock &time){
		
		if(time.mHours >12)
                {
			time.mHours = time.mHours - 12;
                        string display= "PM";
                 }
		else
                 {
			string display= "AM";
                        // time.mHours = time.mHours; // Redundant, since mHours already = mHours.
                 }

			

		out << time.mHours;
		out << ":"<< time.mMinutes <<":"
		<< time.mSeconds  <<string<< endl; 
		return out;
Well when I try it this way it prints the time with no AM or PM added to it. Just 5:40:32 for example
jkelly18

Sorry, I didn't really notice that you don't have a cout statement in this code to print out the time. What you have is a way to save the time into the file. And change the keyword 'string' to 'display', in your out << statement.
Add this before your return out, though I'm not quite sure what you are trying to return. I think you should have a close.out instead, to close the file. Anyway, to print out the time, try.

 
cout << time.mHours << ":"<< time.mMinutes <<":"<< time.mSeconds  << " " << display<< endl;


Well I would try that except my printTimeMessage function is supposed to be what prints it out. I'll put the entire code for clarity.

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

#include <iostream>
#include <string>

using namespace std;

class Clock
{
private:
	int mHours; //(range 0-23)
	int mMinutes; //(range 0-59)
	int mSeconds; //(range 0-59)
public:
	Clock();
	Clock(int);
	Clock(int, int);
	Clock(int, int, int);
	int getHours()  
		{return mHours;}
	int getMinutes()  
		{return mMinutes;}
	int getSeconds()  
		{return mSeconds;}
	void setHours(int hourValue) {
		if ( hourValue >= 0 && hourValue <23) { 
			mHours = hourValue;
		} else if(hourValue<0) { 
			mHours = 0;
		} else if(hourValue>12) {
			mHours = 12;
		}
	}
	void setMinutes(int minuteValue) {
		if ( minuteValue > 0 && minuteValue < 60) {
			mMinutes = minuteValue;
		} else if ( minuteValue < 0) {
			mMinutes = 0;
		} else if ( minuteValue > 59) {
			mMinutes = 59;
		}
	}
	void setSeconds(int secondValue) {
		if ( secondValue > 0 && secondValue < 60) {
			mSeconds = secondValue;
		} else if ( secondValue < 0) {
			mSeconds = 0;
		} else if ( secondValue > 59) {
			mSeconds = 59;
		}
	}
	bool isMorning();
	bool isAfternoon();
	bool isEvening();
	void tick();
	friend ostream &operator<<(ostream &, Clock&);	
};
	ostream &operator<<(ostream &out, Clock &time){
		
		if(time.mHours >12)
		{	time.mHours = time.mHours - 12;
			string display = "PM";
		}else{
			string display = "AM";
		}

		
		out << time.mHours;
		out << ":"<< time.mMinutes <<":"
		<< time.mSeconds << " "<<display << endl; 
		return out;
}
	Clock::Clock() {
		setHours(12);
		setMinutes(0);
		setSeconds(0);
		}
	Clock::Clock(int hourValue) 
		{setHours(hourValue);}
	Clock::Clock(int hourValue, int minuteValue) 
		{setHours(hourValue);
		setMinutes(minuteValue);}
	Clock::Clock(int hourValue, int minuteValue, int secondValue) 
		{setHours(hourValue);
		setMinutes(minuteValue);
		setSeconds(secondValue);}
	

	bool Clock::isMorning() {
		if ( mHours < 12) {
			return true;
		}else{
			return false;
		}
	}
	bool Clock::isAfternoon() {
		if ( mHours >= 12 && mHours < 18) {
			return true;
		}else{ 
			return false;
		}
	}
	bool Clock::isEvening() {
		if ( mHours < 18) {
			return false;
		}else{
			return true;
		}
	}
	void Clock::tick() {
		setSeconds((getSeconds() + 1 ) % 60);
	
		if ( getSeconds()==0)
		{
			setMinutes((getMinutes() + 1) % 60);
			if (getMinutes() == 0)
			{
				setHours((getHours() % 12) +1);
			}
		}
		}

		/*if (mSeconds < 60)
			mSeconds = mSeconds + 1;
		else 
			mSeconds = 0;
		if (mMinutes < 60 && mSeconds == 0)
			mMinutes = mMinutes + 1;
		else 
			mMinutes = 0;
		if (mHours < 24 && mMinutes == 0 && mSeconds == 0)
			mHours = mHours + 1;
		else 
			mHours = 0;*/
		

void printTimeMessage(Clock a) {

		cout << "The current time is "<< a << endl;
}

int main() {
	
	int hour;
	int minute;
	int second;
	Clock a(hour,minute,second);

	cout << "What is the hour?" << endl;
	cin >> hour;
	
	cout << "What is the minute?" << endl;
	cin >> minute;

	cout << "What is the second?" << endl;
	cin >> second;

printTimeMessage(Clock(hour,minute,second));

	hour= hour +2;
	minute = minute + 30;
	second = second + 15;
	cout << "Back to the Future!"; 
	printTimeMessage(Clock(hour,minute,second));

	a.tick();
	printTimeMessage(Clock(hour,minute,second));	

return 0;
}
	
		


I was told to use the Clock class as the argument for the printTimeMessage as well so I can't change that. I'm also having problems with the tick function so if you see some kind of problem with that that you can notice please feel free to let me know.
jkelly18

After adding string display; after
1
2
ostream &operator<<(ostream &out, Clock &time){
		
, the time does get printed using the void printTimeMessage(Clock a) function, though I would add a newline before the cout or change it to cout << endl << "The current time is "<< a << endl;

Not sure what else your program is needing to do at the time. But as it is, it works nice..
Whenever I put the " display" in the out line in ostream &operator<<() it tells me an error "expected primary-expression before 'display' " and "expected ';' before 'display' "
You are declaring the string display inside of the scope of the if statements, so it no longer exists when you go to print it out via cout. Declare it so it is in scope of the cout<< statement and it will work.
I'm not sure if I'm understanding you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

ostream &operator<<(ostream &out, Clock &time){
string display;
if(time.mHours>12){
     time.mHours = time.mHours -12;
     string display = "PM";
}else{
     string display = "AM";
}
out << time.mHours;
out <<":" << time.mMinutes;
out <<":" << time.mSeconds << " " << display << endl;
} 


This creates the time but no AM/PM
I don't think I am allowed to put a cout line in the part of the code as the printTimeMessage is the part that it supposed to be printing.
Your lines 6 and 8 are creating a completely separate string called display, assigning to it, then throwing it away. The string display from line 3 remains empty. You'll want to assign to the string from line 3 on lines 6 and 8 instead of creating a new string.
1
2
3
4
5
6
7
8
9
10
11
12
ostream &operator<<(ostream &out, Clock &time){

if(time.mHours>12){
     time.mHours = time.mHours -12;
     string display = "PM";
}else{
     string display = "AM";
}
out << time.mHours;
out <<":" << time.mMinutes;
out <<":" << time.mSeconds << " " << display << endl;
} 


So like this? Because this causes the error message that 'display' isn't declared in the scope. I'm sorry if I'm being a bit difficult I'm just trying to understand.
Topic archived. No new replies allowed.