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;
}
|