- #1
Bob Busby
- 47
- 0
Homework Statement
Write a program that asks the user to enter the number of seconds as an integer value
(use type long) and that then displays the equivalent time in days, hours, minutes, and
seconds. Use symbolic constants to represent the number of hours in the day, the number of minutes in an hour, and the number of seconds in a minute. The output should
look like this:
Enter the number of seconds: 31600000
31600000 seconds = 365 days, 46 minutes, 40 seconds
I'm a newcomer to programming and c++ and I'm having a bit of trouble with this problem. Everything works beautifully until the computation for newSeconds near the bottom of the code. No matter what value I feed it for "seconds," "secondsFinal" is always 1 less than "newSeconds." I have no idea what is going on. Any help would be appreciated.
Homework Equations
The Attempt at a Solution
#include <iostream>
const double SECONDS_IN_MINUTE = 60;
const double MINUTES_IN_HOUR = 60;
const double HOURS_IN_DAY = 24;
using namespace std;
int main()
{
cout << "Enter a number of seconds: ______\b\b\b\b\b\b";
long seconds;
cin >> seconds;
double days, hours, minutes, newSeconds; //for computation
int daysFinal, hoursFinal, minutesFinal, secondsFinal; //for display
days = seconds / SECONDS_IN_MINUTE / MINUTES_IN_HOUR / HOURS_IN_DAY; //converts seconds to days
daysFinal = int (days); //gets the integer part of days
hours = (days - int (days)) * HOURS_IN_DAY; //gets the noninteger part of days even if days is < or > an integer
hoursFinal = int (hours);
minutes = (hours - int (hours)) * MINUTES_IN_HOUR;
minutesFinal = int (minutes);
newSeconds = (minutes - int (minutes)) * SECONDS_IN_MINUTE;
secondsFinal = int (newSeconds);
cout << seconds << " seconds = " << daysFinal << " days, " << hoursFinal << " hours, " << minutesFinal << " minutes, " << secondsFinal << " seconds.";
return 0;
}