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:

#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
    
    const int SIXTY = 60;
    const int HUNDRED = 100;
    double IMPROVEMENT = .25; // This is to multiply the 25% better time 
    //int start_time = 0;

    int start_time;

    cout << "Enter start time: " << endl;
    cin >> start_time;

    int start_hours = start_time / HUNDRED;
    int start_minutes= start_time % HUNDRED;
    int end_time;

    cout << "Enter end time: " << endl;
    cin >> end_time;

    int end_hours = (end_time / HUNDRED);
    int end_minutes = (end_time % HUNDRED);

    
    int total_minutes = (end_minutes - start_minutes) * IMPROVEMENT;
    int total_hours = end_hours - start_hours;

    total_hours = total_hours / SIXTY;
    
    
    cout.fill('0');
    cout << "The new end time is: " << setw(2) << total_hours  << setw(2) << total_minutes << endl;
    
    return 0;
}

I need help to figure how to make the 'improved' time affect hours and not just minutes.

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

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;

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.

This article has been dead for over six months. Start a new discussion instead.