Class another question

May 2, 2017 at 2:52pm
Hi there

I am doing an exercise and all is fine apart from returning value from Time3 object.
if I print out inside function TimeAdd I get summary of both times.
however I am not sure how to return correctly from function.

I got return temp;
but then when I print out Time3 it does print only zeros??
what I am doing wrong?

should I returnb this as reference?

thanks :)
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
#include <iostream>

class Time
{
private:
	int hours;
	int minutes;
	int seconds;
public:
	Time(): hours(0), minutes(0), seconds(0)
	{}

	Time(int hours_, int minutes_, int seconds_): hours(hours_), minutes(minutes_), seconds(seconds_)
	{}

	void AddTimeM()
	{
		std::cout << "Plase provide time" << std::endl;
		std::cout << "Hours in format HH: ";
		std::cin >> hours;
		std::cout << "Minutes in format MM: ";
		std::cin >> minutes;
		std::cout << "Seconds in format SS: ";
		std::cin >> seconds;
	}

	void Display()
	{
		std::cout << "Time is liquid...." << std::endl;
		std::cout << hours << ":" << minutes << ":" << seconds << std::endl;
	}

	Time TimeAdd(Time time1, Time time2);
};

Time Time::TimeAdd(Time time1, Time time2)
	{ 
		Time temp;
		temp.hours = time1.hours + time2.hours;
		temp.minutes = time1.minutes + time2.minutes;
		temp.seconds = time1.seconds + time2.seconds;

		//std::cout << temp.hours << ":" << temp.minutes << ":" << temp.seconds << std::endl;
		return temp;
	}


int main()
{
	Time Time1, Time3;

	Time Time2(13, 46, 12);

	std::cout << "Time 2: ";
	Time2.Display();

	Time1.AddTimeM();
	
	std::cout << "Time 1: ";
	Time1.Display();
	std::cout << std::endl;

	std::cout << "Time 2: ";
	Time2.Display();

	std::cout << "Time 3: " ;
	Time3.TimeAdd(Time1, Time2); // how to return value from Time3???  
	Time3.Display();



	
	system("pause");
    return 0;
}


May 2, 2017 at 3:24pm
All right xxvms!
you should read your code carefully!
first!
you don't need return time it is ok to have void!
two!

you don't need temporary time variable!!!

so pay attention ;)

corrected version of the code

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
// Classes-Time-exercise-Ch6-ex3.cpp : Defines the entry point for the console application.
//

#include <iostream>

class Time
{
private:
	int hours;
	int minutes;
	int seconds;
public:
	Time(): hours(0), minutes(0), seconds(0)
	{}

	Time(int hours_, int minutes_, int seconds_): hours(hours_), minutes(minutes_), seconds(seconds_)
	{}

	void AddTimeM()
	{
		std::cout << "Plase provide time" << std::endl;
		std::cout << "Hours in format HH: ";
		std::cin >> hours;
		std::cout << "Minutes in format MM: ";
		std::cin >> minutes;
		std::cout << "Seconds in format SS: ";
		std::cin >> seconds;
	}

	void Display()
	{
		std::cout << "Time is liquid...." << std::endl;
		std::cout << hours << ":" << minutes << ":" << seconds << std::endl;
	}

	void TimeAdd(Time time1, Time time2);
};

void Time::TimeAdd(Time time1, Time time2)
	{ 
		
		hours = time1.hours + time2.hours;
		minutes = time1.minutes + time2.minutes;
		seconds = time1.seconds + time2.seconds;

		//std::cout << temp.hours << ":" << temp.minutes << ":" << temp.seconds << std::endl;

	}


int main()
{
	Time Time1, Time3;

	Time Time2(13, 46, 12);

	std::cout << "Time 2: ";
	Time2.Display();

	Time1.AddTimeM();
	
	std::cout << "Time 1: ";
	Time1.Display();
	std::cout << std::endl;

	std::cout << "Time 2: ";
	Time2.Display();

	std::cout << "Time 3: " ;
	Time3.TimeAdd(Time1, Time2);
	Time3.Display();



	
	system("pause");
    return 0;
}
May 2, 2017 at 3:26pm
thank you @xxvms you are great ;)
May 2, 2017 at 3:37pm
Please don't double post.
http://www.cplusplus.com/forum/beginner/214718/

Please delete your other thread.
May 2, 2017 at 6:32pm
@AbstractionAnon - i know what has happened!
when I tried to publish my first post I got message that there was error and my message was not posted.
I tried twice to publish but each time it come back with an error.

So I reloaded page create new post and again I created same post.

This time it worked fine.... but it appears that my post was posted twice!!!
Last edited on May 2, 2017 at 8:19pm
Topic archived. No new replies allowed.