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. =)

Recommended Answers

All 5 Replies

>>She will be met in the afterlife by her husband, Raymond, her son, Paul Jr., and daughter, Ruby.


Yes -- use the mod operator. such as seconds = secs % 60.

Ah! Thanks, you just made me realize how the modulo operator works! I had some serious trouble grasping this yesterday night, well that might've been 'couse I tried to around 3 AM, but still, thanks!

So, for example:

cout << (Sec % (60 *60)) / 60

Would equal to the amount of whole minutes Sec make out.

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.

If you use the same expression more than once, you should consider caching it into a variable.

try this, we know that
1 hr = 60 mins
1 min = 60 secs


so

sec = x

mins = int(sec / 60)
sec = sec % 60

hrs = int(mins/60)
mins = min % 60

days = int(hrs/24)
hrs = hrs%24

hence you get

days hrs mins and sec

Regards
Shaik Akthar

>>mins = int(sec / 60)
Its not necessary to use the typecast. sec/60 always returns an int when sec is an int.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.