seconds to days, hours, minutes, seconds

Time calculator – Write a program that prompts for and reads a number of seconds. If the number of seconds is greater than or equal to 86400, display the number of days, hours, minutes, and seconds; if the number of seconds is greater than or equal to 3600, display the number of hours, minutes, and seconds; and if the number of seconds is greater than or equal to 60, display the number of minutes and seconds. There are 60 seconds in a minute, 3600 seconds in an hour, and 86400 seconds in a day. Use if..else if constructs and consts for the seconds-in-xxx items.

I can't figure out why the number of hours prints before the "there are" statement. Can anyone help? *edit This has been solved but I just noticed that when converting seconds to minutes it is output this way can anyone see why?
"Enter number of seconds: 777
There are 12 minutes, and 0 seconds in 57 seconds
Press any key to continue . . ."


"Enter number of seconds: 10000
2
There are 2 hours, 46 minutes, and 40 seconds in 10000 seconds
Press any key to continue . . ."

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
  #include <iostream>
using namespace std;

const int SECS_MIN = 60;
const int SECS_HR = 60 * 60;
const int SECS_DAY = SECS_HR * 24;

int main()
{
	int secs;
	int rsecs = 0;  // remaining seconds
	int days, hours, mins; 
	cout << "Enter number of seconds: ";
	cin >> secs;
	cout << secs / SECS_HR << endl;
	if (secs >= SECS_DAY) {
		days = secs / SECS_DAY;
		rsecs = secs % SECS_DAY;
		hours = rsecs / SECS_HR;
		rsecs = rsecs % SECS_HR;
		mins = rsecs / SECS_MIN;
		rsecs = rsecs % SECS_MIN;
		cout << "There are " << days << " days, " << hours << " hours, " << mins
			<< " minutes, and " << rsecs << " seconds in " << secs << " seconds\n";
	}
	else if (secs >= SECS_HR) {
		hours = secs / SECS_HR;
		rsecs = secs % SECS_HR;
		mins = rsecs / SECS_MIN;
		rsecs = rsecs % SECS_MIN;
		cout << "There are " << hours << " hours, " << mins
			<< " minutes, and " << rsecs << " seconds in " << secs << " seconds\n";
	}
	else if (secs >= SECS_MIN) {
		mins = secs / SECS_MIN;
		secs = secs % SECS_MIN;
		cout << "There are " << mins
			<< " minutes, and " << rsecs << " seconds in " << secs << " seconds\n";
	}
	else
		cout << " " << secs << " seconds are less than one minute\n";
	return 0;
}
Last edited on
I can't figure out why the number of hours prints before the "there are" statement.

Because you have a cout statement at line 15.
Not sure why I couldn't see that... Thanks
I just noticed that when converting seconds to minutes the output is formatted like this

Enter number of seconds: 777
There are 12 minutes, and 0 seconds in 57 seconds
Press any key to continue . . .

Can anyone see why?

It should appear like
Enter number of seconds: 77899
There are 21 hours, 38 minutes, and 19 seconds in 77899 seconds
Press any key to continue . . .
Nevermind. Solved. Should have been

else if (secs >= SECS_MIN) {
mins = secs / SECS_MIN;
rsecs = secs % SECS_MIN;
Topic archived. No new replies allowed.