take a look!

What's wrong with minutes? Wrong sum! What is my fault?

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
  struct Time
{
	int hrs, mins, secs; 
};
long time_to_secs(Time);
Time secs_to_time(long);
int main()
{
	Time t1, t2, t3; char c;
	cout<<"Enter first time (hh:mm:ss): ";
	cin>>t1.hrs>>c>>t1.mins>>c>>t1.secs;
	cout<<"Enter second time (hh:mm:ss): ";
	cin>>t2.hrs>>c>>t2.mins>>c>>t2.secs;
	t3 = secs_to_time(time_to_secs(t1)+time_to_secs(t2)); 
	cout<<"Hours"<<setw(12)<<"Minutes"<<setw(12)<<"Seconds"<<endl;
	cout<<setw(3)<<t3.hrs<<setw(8)<<":"<<setw(4)<<t3.mins<<setw(4)<<":"<<setw(4)<<t3.secs<<endl;
return 0;
}

long time_to_secs(Time t3)
{
	return t3.hrs*60*60 + t3.mins*60 + t3.secs; 
}

Time secs_to_time(long seconds)
{
	Time r;    
	r.secs = seconds;
	r.mins = seconds / 60;
	r.hrs = seconds / 3600;
	while (r.secs >= 60)
	{
		++r.mins;
		r.secs-=60;
	}
	while (r.mins >= 60)			
		r.mins-=60;
	
	return r;
	}


Minutes sum is wrong.
Last edited on
Take a look.
http://www.catb.org/esr/faqs/smart-questions.html#bespecific

What about
r.mins = (seconds / 60) % 60;

What's the point of both while loops?

Use a debugger.
Step through the code, find out where it goes wrong.

Write code, it doesn't work, dump it on a forum is not a long term strategy.
You need to learn how to fix your own mess.
Line 28 requires a modulus as well:

r.secs = seconds % 60;

Remove the loops.
First of all I appreciate your responses, coder 777, thanks a lot, man.
For you salem c I've understood your words, and I read slowly every single word in the article you attached, and I really appreciate it, you might think I was lazy, maybe I'm, but believe me I did nothing the whole last night but thinking and trying everything I could but these reminders, it's just I needed help, just a hint, thanks a thousand both of you guys and the other ones always help to the self study beginners, and I'll promise to do my best before I ask.
Last edited on
Topic archived. No new replies allowed.