Help with Decision Program

I need some help on an assignment on an introduction to programming class. My goal is to ask the user for a number of seconds and determine how many minutes, hours, and days that is equivalent to, but my program is not working correctly:

#include <iostream>
using namespace std;

int main()
{
// Constants

const double MINUTE = 60;
const double HOUR = 3600;
const double DAY = 86400;

// Variable of user input
int SECONDS;


cout << "We are going to calculate the time: \n";

cout << "Enter the number of seconds: \n";
cin >> SECONDS;

// Calculate the total
double totalminutes = SECONDS / MINUTE;
double totalhour = SECONDS / HOUR;
double totalday = SECONDS / DAY;

// Determine the result
if (SECONDS >= MINUTE)
{
cout << totalminutes << " minutes \n";
}
else if (totalhour >= HOUR)
{
cout << totalhour << " hours \n";
}
else if (totalday >= DAY)
{

cout << totalday << " days \n";
}
system("pause");

return 0;
}
integer math is probably better here. If you used all integers...


int days = input/DAY;
int hours = (input%DAY)/HOUR;
int min = ((input%DAY)%HOUR)/MINUTE;

I think? Ive been known to mess up stuff like this :)

What you are doing is diving the total by each conversion, not what is left after you remove that part. for example, 1 day, 1 hour, and 1 min, you convert that into 25 hours ... you forgot to remove the 1 day from it, see?



1
2
3
double totalminutes = SECONDS / MINUTE;
double totalhour = SECONDS / HOUR;
double totalday = SECONDS / DAY;

Don't use doubles for calculations that can be represented integrally. Use ints instead.
Your maths is incorrect here. Consider when SECONDS is 3660.
totalminutes = 3660 / 60 = 61
totalhour = 3660 / 3600 = 1 (integer division)
Obviously, 3660 seconds isn't 1 hour and 61 minutes. It is 1 hour and 1 minute. A simple way to fix this would be to calculate days, then hours then minutes, in that order. Find how many whole periods can fit and subtract from the total seconds.
For example, using the above with SECONDS = 3660,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
totalday = SECONDS / DAY
         = 3660 / 86400
         = 0
// subtract amount of whole days from total
SECONDS = SECONDS - totalday * DAY
        = 3660 - 0
        = 3660

totalhour = SECONDS / HOUR
          = 3660 / 3600
          = 1
// subtract amount of whole hours from total
SECONDS = SECONDS - totalhour * HOUR
        = 3660 - 1 * 3600
        = 60

totalminutes = SECONDS / MINUTE
             = 60 / 60
             = 1
// get the idea?
SECONDS = SECONDS - totalminutes * MINUTE
        = 60 - 1 * 60
        = 0


You could also use modulo as mentioned above, but do whatever is most intuitive to you.
#include<iostream>
#include<complex>

using namespace std;

int main()
{
// Store a generalized lambda, that
long s=0,m=0,h=0,d=0;
cout<<"enter in seconds";
cin>>s;
cout<<endl<<s;
m=s / 60;
s=s %60;


if(m>=60){
h=m / 60;
m=m%60;
if(h>=24){
d=h/ 24;
h=h %24;
}
}
cout<<endl<<d<<" days"<< h<<"hours"<< m<<"mins"<<s<<"sec";
return 0;
}
Topic archived. No new replies allowed.