if statements and modulus

I was given the assignment to write a program that asks the user to enter a number of seconds.
-There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or
equal to 60, the program should display the number of minutes in that many seconds.
-There are 3600 seconds in an hour. If the number of seconds entered by the user is greater than
or equal to 3600, the program should display the number of hours in that many seconds.
-There are 86400 seconds in a day. If the number of seconds entered by the user is greater than or
equal to 86400, the program should display the number of days in that many seconds.

and the output has to look like this:
7255 seconds = 2 hours 55 seconds

My code looks like this and my output doesn't look right and im not sure how to fix it.
my output:In 86780 seconds there are 380 days,-4195440 hours,5 minutes and -32745220 seconds

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

int main()
{
	int totalSeconds, remainingSeconds, days, hours, minutes, seconds;
	cout<<"Please enter a number of seconds";
	cin>>totalSeconds;
	
	remainingSeconds=totalSeconds;
 
	if(remainingSeconds>=86400)
	{
		days=remainingSeconds%86400;
		remainingSeconds=remainingSeconds-(days*86400);
	}
	else if(remainingSeconds>=3600)
    {
		hours=remainingSeconds%3600;
		remainingSeconds=remainingSeconds-(hours*3600);
	}
	else if(remainingSeconds>=60)

	{
		minutes=remainingSeconds%60;
		remainingSeconds=remainingSeconds-(minutes*60);
	}
	else
	{
		remainingSeconds=remainingSeconds;
	}

	cout<<"In "<<totalSeconds<<" seconds there are "<<days<<" days,"<<hours<<" hours,"<<minutes<<" minutes and "<<remainingSeconds<<" seconds"<<endl;
	
	return 0;
}
To get days, hours, minutes, seconds, use the operator /.
To get the remaining seconds, only use the operator %.

Avoid magic numbers as much as possible (86400, 3600, etc).
closed account (LA48b7Xj)
Sakura is right, here is how you might write the program...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
	cout << "Enter number of seconds: ";

	int seconds;
	cin >> seconds;

	const int minutes = seconds / 60;
	const int hours = minutes / 60;
	const int days = hours / 24;

	cout << days << " days " << hours % 24 << " hours " 
		 << minutes % 60 << " minutes " << seconds % 60 << " seconds\n";  
}

@jeg19 - If you want to follow your current thinking, then I suggest one minor change: Remove the word "else" from your code. If you use "else", only one of those blocks will execute. Instead, you want each block of your code to execute in succession (peel off the number of whole days, then move to hours, then move to minutes, etc...). The last block (line 32) is unnecessary.
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int totalSeconds;
    int remainingSeconds;
    int days = 0;
    int hours = 0;
    int minutes = 0;

    cout<<"Please enter a number of seconds";
    cin>>totalSeconds;

    remainingSeconds=totalSeconds;

    if(remainingSeconds>=86400)
    {
        days=remainingSeconds/86400;
        remainingSeconds=remainingSeconds-(days*86400);
    }

    if(remainingSeconds>=3600)
    {
        hours=remainingSeconds/3600;
        remainingSeconds=remainingSeconds-(hours*3600);
    }

    if(remainingSeconds>=60)
    {
        minutes=remainingSeconds/60;
        remainingSeconds=remainingSeconds-(minutes*60);
    }

    cout<<"In "<<totalSeconds<<" seconds there are "<<days<<" days,"<<hours<<" hours,"<<minutes<<" minutes and "<<remainingSeconds<<" seconds"<<endl;

    return 0;
}


Edit: It is also a good idea to initialize your variables. Let's say you enter less than 86400. Then the block that sets "days" never executes, and you'll end up printing whatever garbage is in there when it is created.

Edit 2: Good catch @Handy Andy. I eyeballed the code and didn't run it. The % operators should be /.
Last edited on
Hello jeg19,

All I did is change % 86400 to / 86400, also the same for the rest. You will also need to change the else if lines to just if statements. Otherwise once you finish with the days if nothing else will be checked.

Line 30 to 33 have no useful purpose they can be deleted. There is no point to set remainingSeconds=remainingSeconds;.

Hope that helps,

Andy
Thank you all so much for your help!!
I have to have an error message for if someone inputs a negative number, i tried to add this right under the last if statement but when i ran it it printed "Error, no negative numbers
In -889 seconds there are 0 days,0 hours,0 minutes and -889 seconds"

if(remainingSeconds>=60)

{
minutes=remainingSeconds/60;
remainingSeconds=remainingSeconds-(minutes*60);
}
else
cout<<"Error, no negative numbers"<<endl;

how do i get it to not print the stuff after the error message for negative inputs
Last edited on
Hello jeg19,

You have two choices for dealing with a negative number:
1
2
3
4
5
6
if (remainingSeconds < 0)
{
	cout << "Error, no negative numbers" << endl;
	Sleep(3000);  //  wait 3 seconds #include Windows.h
	exit(1);
}

which I would put as the first if because if it is a negative number there is no point in going the rest.

or you could use:
1
2
if (remainingSeconds > -1)
	cout << "In " << totalSeconds << " seconds there is " << year << " year " << days << " days, " << hours << " hours, " << minutes << " minutes and " << remainingSeconds << " seconds" << endl;

which works, but you go through the whole program just to print nothing.

Hope that helps,

Andy
Topic archived. No new replies allowed.