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:
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?
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,