
-----------------------------------
kentlah
Tue Mar 20, 2007 7:45 pm

Outputting time into sections
-----------------------------------
(I'm using dev-C++ 4.9.9.2)

Hi, I'm new here (obviously :P) but my background isn't important :P...

I just started learning C++ right now at university and one of my tasks is to create a program that takes in speeds (in km/h) and distances (km) and uses these to find the time taken to travel the distance. This part isn't terribly hard because all it requires is simple arithmetic functions... My only gripe right now is that I have to output the time into the following format:

hh : mm : seconds (to one decimal place)

So as of now I'm stuck and just need a nudge in the right direction or even if someone is willing to tell me exactly how to do it I'm all ears (eyes? O_o)

Cheers

-----------------------------------
Andy
Tue Mar 20, 2007 8:11 pm

Re: Outputting time into sections
-----------------------------------
Hmm, well this sounds doable. I think I might be able to help if you post some of your code.

-----------------------------------
md
Tue Mar 20, 2007 8:32 pm

RE:Outputting time into sections
-----------------------------------
do your calculations in seconds, then 1h == 60m, 1m == 60s. and precision can be set with the setprecision function. Google it for more info as I do not recall exact parameters.

-----------------------------------
kentlah
Tue Mar 20, 2007 9:07 pm

Re: Outputting time into sections
-----------------------------------
int speedInKmh1, speedInKmh2, speedInKmh3;
   int distanceInKm1, distanceInKm2, distanceInKm3;
   
   cout  speedInKmh1 >> speedInKmh2 >> speedInKmh3;
   cout  distanceInKm1 >> distanceInKm2 >> distanceInKm3;
   
   
   int swimtimeHour, swimtimeMin, swimtimeSec;
   int cyctimeHour, cyctimeMin, cyctimeSec;
   int runtimeHour, runtimeMin, runtimeSec;
   
   swimtimeHour = distanceInKm1 / speedInKmh1; //to get the time in hours
   swimtimeMin = (distanceInKm1 * 60) / speedInKmh1; //to get the time in minutes
   swimtimeSec = (distanceInKm1 * 3600) / speedInKmh1; //to get the time in seconds
   
   cyctimeHour = distanceInKm2 / speedInKmh2; //to get the time in hours
   cyctimeMin = (distanceInKm2 * 60) / speedInKmh2; //to get the time in minutes
   cyctimeSec = (distanceInKm2 * 3600) / speedInKmh2; //to get the time in seconds
   
   runtimeHour = distanceInKm3 / speedInKmh3; //to get the time in hours
   runtimeMin = (distanceInKm3 * 60) / speedInKmh3; //to get the time in minutes
   runtimeSec = (distanceInKm3 * 3600) / speedInKmh3; //to get the time in seconds
   
   
   cout 