C++ – Converting seconds to hours and minutes and seconds

c

#include <cstdio>
#include <iostream>
using namespace std;
int main ()
{
    int seconds, hours, minutes;
    cin >> seconds;
    hours = seconds/3600;
    cout << seconds << " seconds is equivalent to " << int(hours) << " hours " << seconds%(hours*60) 
         << " minutes " << (seconds%(hours*3600))-((seconds%(hours*60))*60) << " seconds.";
}

For some reason, this program works with only numbers above 3600. Does anyone know how to fix this problem? Whenever I do a number below 3600, the screen shows up with a message from Windows saying that the program has stopped working.

Best Answer

Try this out instead, tested and works:

int seconds, hours, minutes;
cin >> seconds;
minutes = seconds / 60;
hours = minutes / 60;
cout << seconds << " seconds is equivalent to " << int(hours) << " hours " << int(minutes%60) 
     << " minutes " << int(seconds%60) << " seconds.";

Because minutes is seconds/60, dividing it by 60 again is equivalent to diving seconds by 3600, which is why it works.