I'm working on some C++ excercies in my book and now I'm supposed to write a program that converats a number of seconds into days, hours, minutes and seconds with the help of arithmetic operators and integer and float variables. Nothing to fancy that is.
It ended up looking something like this:
#include <iostream>
using namespace std;
int main ()
{
long Sec;
const int HouPDay = 24;
const int MinPHou = 60;
const int SecPMin = 60;
cout << "Type in the number of seconds: ";
cin >> Sec;
cout << "\n" << Sec << " seconds = "
<< Sec / (HouPDay * MinPTim * SecPMin) << " days, ";
//Calculates and prints the number of whole days that Sek make out
cout << (Sec % (HouPDay * MinPHou * SecPMin))/(MinPHou * SecPMin)
//Calculates and prints the number of whole hours that the remainder of the previous argument make out
<< " hours, ";
cout << ((Sec % (HouPDay * MinPHou * SecPMin))%(MinPHou * SecPMin))/SecPMin
// Calculates and prints the number of whole minutes the the remainder of the previous argument make out
<< " minutes and ";
cout << ((Sec % (HouPDay * MinPTim * SecPMin))%(MinPTim * SecPMin))%SecPMin
// Lastly; calculates and prints the number of whole seconds that're still left.
<< " seconds.\n";
}
It feels like it's very inefficient coding considering the program has to repeat the same arithemtic argument for every "cout" and that's why I ended up posting this, I want to know whether it can improved without getting to advanced.
And yea, the variable containing the number of seconds had to be a long integer but if the code can be improved if that condition is overseen please tell. =)