943,799 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1711
  • C++ RSS
Sep 17th, 2006
1

Noob Help needed for Time problem

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
matrimforever is offline Offline
67 posts
since Sep 2006
Sep 18th, 2006
1

Re: Noob Help needed for Time problem

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
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Sep 18th, 2006
1

Re: Noob Help needed for Time problem

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;
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Sep 18th, 2006
1

Re: Noob Help needed for Time problem

Hmm, am i converting that right? Am i making it too difficult ?
Last edited by matrimforever; Sep 18th, 2006 at 12:24 am.
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
matrimforever is offline Offline
67 posts
since Sep 2006
Sep 18th, 2006
0

Re: Noob Help needed for Time problem

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.
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Sep 18th, 2006
1

Re: Noob Help needed for Time problem

Thank you for your help that did the trick I think. Much appreciated!
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
matrimforever is offline Offline
67 posts
since Sep 2006
Sep 18th, 2006
0

Re: Noob Help needed for Time problem

no problem
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Determine if a certain ivalue is in a string
Next Thread in C++ Forum Timeline: Problem with VC++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC