Noob Help needed for Time problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Noob Help needed for Time problem

 
1
  #1
Sep 17th, 2006
I am trying to write a simple program in c++ to take a beginning delivery time in 24 hour time and an ending delivery time then taking 25% off that time(making the end time 25% sooner). For example, i'd have a user enter in 0900 for start time and 1030 for an end time. The output for the new end time is 1007. My code sort of works, but I don't know how to incorporate the 25% sooner data.

Here it is:

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main()
  6.  
  7. {
  8.  
  9. const int SIXTY = 60;
  10. const int HUNDRED = 100;
  11. double IMPROVEMENT = .25; // This is to multiply the 25% better time
  12. //int start_time = 0;
  13.  
  14. int start_time;
  15.  
  16. cout << "Enter start time: " << endl;
  17. cin >> start_time;
  18.  
  19. int start_hours = start_time / HUNDRED;
  20. int start_minutes= start_time % HUNDRED;
  21. int end_time;
  22.  
  23. cout << "Enter end time: " << endl;
  24. cin >> end_time;
  25.  
  26. int end_hours = (end_time / HUNDRED);
  27. int end_minutes = (end_time % HUNDRED);
  28.  
  29.  
  30. int total_minutes = (end_minutes - start_minutes) * IMPROVEMENT;
  31. int total_hours = end_hours - start_hours;
  32.  
  33. total_hours = total_hours / SIXTY;
  34.  
  35.  
  36. cout.fill('0');
  37. cout << "The new end time is: " << setw(2) << total_hours << setw(2) << total_minutes << endl;
  38.  
  39. return 0;
  40. }
I need help to figure how to make the 'improved' time affect hours and not just minutes.
Last edited by matrimforever; Sep 17th, 2006 at 11:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Noob Help needed for Time problem

 
1
  #2
Sep 18th, 2006
Logically...you have a start time and an end time stored in variables.

create a third variable to hold the total time (start time - end time)

multiply the total time by 0.75 (totalTime*0.75) and store it in a new variable

add the calculated time to the start time

mathematically...

((end time - start time) * 0.75) + start time
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Noob Help needed for Time problem

 
1
  #3
Sep 18th, 2006
Ah..I just saw the bottom part...the minute hour thing is kind of tricky

convert your start and end time to minutes...so that all of your calculations are done in minutes...

the final step would be to convert the minutes back into an hour:minute format

so hours would be calculated by

int hours = newTime / 60
int minutes = newTime % 60

and your time output would be

cout << hours << ":" << munutes;
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Noob Help needed for Time problem

 
1
  #4
Sep 18th, 2006
Hmm, am i converting that right? Am i making it too difficult ?
Last edited by matrimforever; Sep 18th, 2006 at 12:24 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Noob Help needed for Time problem

 
0
  #5
Sep 18th, 2006
Kind of...the best thing to do is write down the process first

get start time in military format
convert start time to minutes
int startMin = ((startTime / 100) * 60) + startTime % 100

get end time in military format
convert end time to minutes
int endMin = ((endTime / 100) * 60) + endTime % 100

Calculate total time
TotalMin = (endMin - startMin)

Calculate improved time
ImproveMin = TotalMin * 0.75

calculate improved time
fastTime = startMin + ImproveMin

Convert the faster time to military format
fastHour = fastTime / 60
fastMin = fastTime % 60
milTime = (fastHour * 100) + (fastMin)

This is just an example...your final output can be done in a couple of ways.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Noob Help needed for Time problem

 
1
  #6
Sep 18th, 2006
Thank you for your help that did the trick I think. Much appreciated!
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Noob Help needed for Time problem

 
0
  #7
Sep 18th, 2006
no problem
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1473 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC