Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Outputting time into sections
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kentlah




PostPosted: Tue Mar 20, 2007 7:45 pm   Post subject: Outputting time into sections

(I'm using dev-C++ 4.9.9.2)

Hi, I'm new here (obviously Razz) but my background isn't important Razz...

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
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Tue Mar 20, 2007 8:11 pm   Post subject: 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




PostPosted: Tue Mar 20, 2007 8:32 pm   Post subject: 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




PostPosted: Tue Mar 20, 2007 9:07 pm   Post subject: Re: Outputting time into sections

Quote:
int speedInKmh1, speedInKmh2, speedInKmh3;
int distanceInKm1, distanceInKm2, distanceInKm3;

cout << "Enter the respective speeds in km/h: " << flush;
cin >> speedInKmh1 >> speedInKmh2 >> speedInKmh3;
cout << "Enter the respective distances in km: " << flush;
cin >> 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 << swimtimeHour << " " << swimtimeMin << " " << swimtimeSec << endl;
cout << cyctimeHour << " " << cyctimeMin << " " << cyctimeSec << endl;
cout << runtimeHour << " " << runtimeMin << " " << runtimeSec << endl;


Pretty messy right now but it's a start. Am I using the right variables for the seconds? I'm supposed to have it to one decimal place. (Only basic as I'm starting out Very Happy )
Andy




PostPosted: Tue Mar 20, 2007 10:31 pm   Post subject: Re: Outputting time into sections

You didnt actually calculate the correct timing.

the correct formula should be:

hour = speed/dist
min = speed*60/dist - hour*60
sec = speed *3600/dist - hour*3600 - min*60

you need to isolate the only minute and second portion of the time.

then use md's setprecision idea to get it to 1 decimal place
kentlah




PostPosted: Tue Mar 20, 2007 11:06 pm   Post subject: RE:Outputting time into sections

Brilliant! Very Happy

Cheers for the help guys, now I've converted 3 separate times and now I just have to add them all up to find a total time... If I have any gripes, I know where to go Wink
kentlah




PostPosted: Tue Mar 20, 2007 11:42 pm   Post subject: RE:Outputting time into sections

Ok, now this is rather embarrassing but how do I set it so that when I get the result, if theres nothing in the minutes box it only displays 0, and I need 00 in there :S
kentlah




PostPosted: Tue Mar 20, 2007 11:54 pm   Post subject: RE:Outputting time into sections

And the program seems unable to handle decimal inputs, how do I fix this?
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Wed Mar 21, 2007 12:42 am   Post subject: Re: Outputting time into sections

for your first problem, look up setw and setfill from iomanip.

for your second problem, use the double type to store your values instead of integer
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: