Apr 2, 2008 at 4:34am UTC
can someone help me please in this program:
write c++ program that prompts the user to input the elapsed time for an event in seconds. the program then outputs the elapsed time in hours,minutes, and seconds.(for example if the elapsed time is 9630 in seconds, then the output is 2:40:30)
thanks alot
Apr 2, 2008 at 7:01am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
using namespace std;
int main() {
int time;
cout << "Enter time elapsed, in seconds: " ;
cin >> time;
cout << (int )time/3600 << ":" ;
if ((int )((time-(((int )time/3600)*3600))/60)<10) cout << "0" ;
cout << (int )((time-(((int )time/3600)*3600))/60) << ":" ;
if (time-((((int )time/3600)*3600)+((int )((time-(((int )time/3600)*3600))/60)*60))<10)
cout << "0" ;
cout << time-((((int )time/3600)*3600)+((int )((time-(((int )time/3600)*3600))/60)*60))
<< endl;
return 0;
}
Last edited on Apr 2, 2008 at 7:03am UTC
Apr 3, 2008 at 6:36pm UTC
While psault's code works, it is very hard to follow. Here is a simpler way to do it that is easier to follow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
using namespace std;
int main()
{
int hours;
int minutes;
int seconds;
int time = 0;
cout << "Enter time elapsed, in seconds: " ;
cin >> time;
cout << endl;
hours = static_cast <int >(time / 3600);
time = time - hours * 3600;
minutes = static_cast <int >(time / 60);
time = time - minutes * 60;
seconds = time;
cout << hours;
if (minutes < 10)
cout << ":0" << minutes;
else
cout << ":" << minutes;
if (seconds < 10)
cout << ":0" << seconds;
else
cout << ":" << seconds;
cout << endl;
return 0;
}
Last edited on Apr 3, 2008 at 6:43pm UTC
Apr 4, 2008 at 7:43am UTC
thx alot for helping me:)