How to add the time into function

I have created this code with the classes for time and message. Basically the point of this code is to give the information of the sender, recipient, time, and message and im having issues with the time part. I was hoping someone could help me.

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
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
#include <vector>
#include <cassert>

using namespace std;

#ifndef CCC_TIME_H

#define CCC_TIME_H

class Time
{
public:

	Time(int hour, int min, int sec);
	Time();
	int get_hours() const;
	int get_minutes() const;
	int get_seconds() const;
	int seconds_from(Time t) const;
	void add_seconds(int s);


private:
	int time_in_secs;
};

/**
   Computes the correct remainder for negative dividend
   @param a - an integer
   @param n - an integer > 0
   @return the mathematically correct remainder r such that
   a - r is divisible by n and 0 <= r and r < n
*/
int remainder(int a, int n)
{
   if (a >= 0)
      return a % n;
   else
      return n - 1 - (-a - 1) % n;
}

Time::Time(int hour, int min, int sec)
{
   assert(0 <= hour);
   assert(hour < 24);
   assert(0 <= min);
   assert(min < 60);
   assert(0 <= sec);
   assert(sec < 60);

   time_in_secs = 60L * 60 * hour + 60 * min + sec;
}

Time::Time()
{
   time_t now = time(0);
   struct tm& t = *localtime(&now);
   time_in_secs = 60L * 60 * t.tm_hour + 60 * t.tm_min + t.tm_sec;
}

int Time::get_hours() const
{
   return time_in_secs / (60 * 60);
}

int Time::get_minutes() const
{
   return (time_in_secs / 60) % 60;
}

int Time::get_seconds() const
{
   return time_in_secs % 60;
}

int Time::seconds_from(Time t) const
{
   return time_in_secs - t.time_in_secs;
}

void Time::add_seconds(int s)
{
   const int SECONDS_PER_DAY = 60 * 60 * 24;
   time_in_secs = remainder(time_in_secs + s, SECONDS_PER_DAY);
}

#endif


class Message{

public:
    Message(string s, string r);
    void append(string m);
    string to_string();
    void print();  //use to_string()
    void setTime(Time& t);

private:
    string recipient;
    string sender;
    string message;
    Time stamp;
};

Message::Message(string s, string r)
{
	sender = s;
	recipient = r;
	message = "";
}

void Message::append(string m)
{
	message += m;
}

string Message::to_string()
{
	return "From: " + sender + "\nTo: " + recipient + "\nTime: " + "\nMessage: " + message;
}

void Message::print()
{
	cout << to_string() <<endl;
}

void Message::setTime(Time& t)
{
	stamp = Time();
}

int main ()
{
	Message m = Message("Harry Hacker", "Rudolf Reindeer");
	m.append("Merry Christmas\n");
	m.append("Happy New Year");
	m.print();
}


so when you run this you can see i left a spot for the time but I just cant figure out how to figure out the Time part of this.
So you need to calculate the seconds, minutes and hours from time_in_secs. It's probably easiest if you calculate them in order from smallest to largest unit. Start with the seconds. When you have caclulated the seconds think how you can calculate the minutes, and so on.
Last edited on
This is how the time will be displayed!. Just a little modification of your function string Message::to_string() is done and included <sstream> to convert int to string in a quick and efficient manner:

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
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
#include <vector>
#include <cassert>
#include <sstream>

using namespace std;

#ifndef CCC_TIME_H

#define CCC_TIME_H

class Time
{
public:

	Time(int hour, int min, int sec);
	Time();
	int get_hours() const;
	int get_minutes() const;
	int get_seconds() const;
	int seconds_from(Time t) const;
	void add_seconds(int s);


private:
	int time_in_secs;
};

/**
   Computes the correct remainder for negative dividend
   @param a - an integer
   @param n - an integer > 0
   @return the mathematically correct remainder r such that
   a - r is divisible by n and 0 <= r and r < n
*/
int remainder(int a, int n)
{
   if (a >= 0)
      return a % n;
   else
      return n - 1 - (-a - 1) % n;
}

Time::Time(int hour, int min, int sec)
{
   assert(0 <= hour);
   assert(hour < 24);
   assert(0 <= min);
   assert(min < 60);
   assert(0 <= sec);
   assert(sec < 60);

   time_in_secs = 60L * 60 * hour + 60 * min + sec;
}

Time::Time()
{
   time_t now = time(0);
   struct tm& t = *localtime(&now);
   time_in_secs = 60L * 60 * t.tm_hour + 60 * t.tm_min + t.tm_sec;
}

int Time::get_hours() const
{
   return time_in_secs / (60 * 60);
}

int Time::get_minutes() const
{
   return (time_in_secs / 60) % 60;
}

int Time::get_seconds() const
{
   return time_in_secs % 60;
}

int Time::seconds_from(Time t) const
{
   return time_in_secs - t.time_in_secs;
}

void Time::add_seconds(int s)
{
   const int SECONDS_PER_DAY = 60 * 60 * 24;
   time_in_secs = remainder(time_in_secs + s, SECONDS_PER_DAY);
}

#endif


class Message{

public:
    Message(string s, string r);
    void append(string m);
    string to_string();
    void print();  //use to_string()
    void setTime(Time& t);

private:
    string recipient;
    string sender;
    string message;
    Time stamp;
};

Message::Message(string s, string r)
{
	sender = s;
	recipient = r;
	message = "";
}

void Message::append(string m)
{
	message += m;
}

string Message::to_string()
{
    //Creating an INstance of type Time
    Time time_now;
    int h = time_now.get_hours();
    int m = time_now.get_minutes();
    int s = time_now.get_seconds();
    
    //Storing Strings into Array of Strings , 0th Index has hours , 1st has minutes and 2nd has seconds
    string hms[3];
    
    ostringstream convert;
    
    convert << h;
    hms[0] = convert.str();
    convert.str(""); //Clearing Buffer
    
    convert << m;
    hms[1] = convert.str();
    convert.str("");
    
    convert << s;
    hms[2] = convert.str();
    convert.str("");
    
    //Converting Integers to String
    
	return "From: " + sender + "\nTo: " + recipient + "\nTime: " + hms[0] + ":" + hms[1] + ":"+ hms[2] +"\nMessage: " + message;
}

void Message::print()
{
	cout << to_string() <<endl;
}

void Message::setTime(Time& t)
{
	stamp = Time();
}

int main ()
{
	Message m = Message("Harry Hacker", "Rudolf Reindeer");
	m.append("Merry Christmas\n");
	m.append("Happy New Year");
	m.print();
}
Topic archived. No new replies allowed.