Hello, I am supposed to create a program that converts time in seconds into minutes, hours, days, weeks, months, years, decades, century, and millennia. I have already emailed my prof and don't expect to get a response (because he usually takes at least a week to respond, if he does at all) and I've already visited the tutor center and they could not help me either, they didn't know how to fix it :\ so I hope someone here could help...
Basically here is what is happening is that everything works okay until seconds need to be converted to centuries, then it either outputs nothing (if seconds ==> century is the first user input) or it loops infinitely (if seconds ==> century is input after an amount of seconds has already been converted).
constint seconds_per_minute = 60;
constint minutes_per_hour = 60;
constint hours_per_day = 24;
int seconds = 0;
int minutes = 0;
int hours = 0;
int days = 0;
cout << "Enter number of seconds to be converted: ";
cin >> seconds;
while (seconds >= 60) //once you are under 60 seconds, no longer need to convert
{
seconds = seconds - seconds_per_minute; //subtracts number of seconds in a minute off
minutes = (minutes + 1)%minutes_per_hour; //adds another minute until it reaches the number of minutes in an hour, then resets to zero
if( minutes == 0 ) //minutes re-rolled, add an hour
{
hours = (hours + 1)%hours_per_day;
if( hours == 0 ) //hours re-rolled, add a day
{
//and so on and so forth...
}
}
}
EDIT: Actually I was thinking about it, and while something like this would work it's way slower than needs to be for this program. I would just do something with modular arithmetic, starting by converting seconds into millennia, subtracting off the number of millenia, move onto years, and do the same thing.
Is a long 32 bits or 64 on your system? To find out add cout << "long is " << sizeof(long) << " bytes\n";
to your output. If it's 32 bits then you are simply overflowing the size of a long. a signed 32 bit number can only hold about 72 years worth of seconds.
Incidentally, this is why I plan to come out of retirement and make gobs of money in the 2030's leading up to the 2034(?) bug when 32-bit C and C++ programs will overflow the time_t value. If you thought Y2K was bad, just wait :).