Computing Time delay

A user wishes to enter a starting time AM/PM format, and delay time in hours and minutes, and have the computer compute and display the time (in AM/PM format) at the end of the delay.
This process should repeat until the user enters 999 for the hours portion of the start time.
Note: The user will enter the start time as two integers and either 'A' 'a' 'P' 'p' with a space (or spaces in-between them, the first representation the hours portion of the time , and second representation the minutes portion of the time and the characters representing either AM or PM. The delay timed will be entered in a similar way.

Example: if the user enters the time 10 13 A
and a delay time of 2 05
then the computer should display 12:18pm as the time at the end of the delay.





The program is by no means finished but I have no clue where to go from where I am at in the program. It could all be wrong for what I know. Any help would be appreciated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Programming project #2 

#include <iostream>


void computeTimeDifference(int& H, int& M, int& H1, int& M2, int AP )
int main()
{
int H;
int M;
int H1;
int M2;
int AP;

    cout << "Enter start time." << endl;
    cout << "Enter hours and minutes aswell as either A for AM or P for PM: ";
    cin >> H >> M >> AP >> endl;

    cout << "Enter delay time." << endl;
    cout << "Enter hours and minutes: ";
    cin >> H1 >> M2 >> endl;

    computeTimeDifference(int& H, int& M, int& H1, int& M2, int AP);

    cout << endl << "Delay Time: " <<  << ":" << endl;
    return 0;
}
void computeTimeDifference(int& H, int& M, int& H1, int& M2, int AP)
{  
Last edited on
Using the library (input validation elided for brevity):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <ctime>
#include <iomanip>

int main()
{
    int start_hr, start_minute ;
    char am_or_pm ;
    // std::cout << "enter start time as hr min a/p: " ;
    std::cin >> start_hr >> start_minute >> am_or_pm ;
    std::cout << "start: " << start_hr << ':' << start_minute << ' ' << am_or_pm << '\n' ;
    if( am_or_pm == 'P' || am_or_pm == 'p' ) start_hr += 12 ; // make it a 24-hr clock

    int delay_hr, delay_minute ;
    // std::cout << "enter delay hr min: " ;
    std::cin >> delay_hr >> delay_minute ;
    std::cout << "delay: " << delay_hr << " hours, " << delay_minute << " minutes\n" ;

    // http://en.cppreference.com/w/cpp/chrono/c/time
    const std::time_t now = std::time(nullptr) ; // get the current time point

    // convert it to (local) calendar time
    // http://en.cppreference.com/w/cpp/chrono/c/localtime
    std::tm tm = *std::localtime( std::addressof(now) ) ;

    // set the end time (start time + delay )
    tm.tm_hour = start_hr + delay_hr ;
    tm.tm_min = start_minute + delay_minute ;

    // the hour and minute values in calendar_time may be out of range
    // normalise it by converting to std::time_t and back
    // http://en.cppreference.com/w/cpp/chrono/c/mktime
    const std::time_t end_time = std::mktime( std::addressof(tm) ) ;
    const std::tm end_calendar_time = *std::localtime( std::addressof(end_time) ) ;

    // format and print out the result as hh:MM AM/PM
    // http://en.cppreference.com/w/cpp/io/manip/put_time
    std::cout << std::put_time( std::addressof(end_calendar_time), "end time: %I:%M %p\n" ) ;
}

http://coliru.stacked-crooked.com/a/8559dcd8aa343ba2
I have to use a function in the program as well and the program can;t have any global variables.
Without using the date and time facilities in the library (input validation elided for brevity):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>

int minutes_since_midnight( int hh, int mm )
{ return ( hh*60 + mm ) % (24*60); }

int hour( int minutes_since_midnight )
{ return minutes_since_midnight % (24*60) / 60 ; }

int minute( int minutes_since_midnight )
{ return minutes_since_midnight % 60 ; }

int main()
{
    int start_hr, start_minute ;
    char am_or_pm ;
    std::cout << "enter start time as hr min a/p: " ;
    std::cin >> start_hr >> start_minute >> am_or_pm ;
    std::cout << "start: " << start_hr << ':' << start_minute << ' ' << am_or_pm << '\n' ;
    if( am_or_pm == 'P' || am_or_pm == 'p' ) start_hr += 12 ; // make it a 24-hr clock

    int delay_hr, delay_minute ;
    std::cout << "enter delay hr min: " ;
    std::cin >> delay_hr >> delay_minute ;
    std::cout << "delay: " << delay_hr << " hours, " << delay_minute << " minutes\n" ;

    const int mins_since_midnight = minutes_since_midnight( start_hr+delay_hr, start_minute+delay_minute ) ;

    int end_hr = hour(mins_since_midnight) ;
    const bool pm = end_hr > 12 ;
    if(pm) end_hr -= 12 ;

    const int end_min = minute(mins_since_midnight) ;

    std::cout << "end time: " << std::setfill('0') << std::setw(2) << end_hr << ':'
              << std::setw(2) << end_min << ( pm ? " PM\n" : " AM\n" ) ;
}
Okay I understand most of that program except for the functions at the beginning and the const int in the middle of the program. I appreciate your help greatly ,but I just want to understand what these things mean In the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// given an input of the time of day in hours and minutes
// returns the number of minutes elapsed since midnight
// for instance, if the input is 10 hours, 30 minutes 
// return 630 ( 10*60 + 30 )
// if the input is out of range, say 27 hours, 72 minutes,
// it is adjusted appropriately: 27:72 == 28:12 == 04:12 on the next day
// we do this by dividing the result by the total number of minutes
//  in a day (24*60) and taking the remainder  
int minutes_since_midnight( int hh, int mm )
{ return ( hh*60 + mm ) % (24*60); }

// given an input of the number of minutes elapsed since midnight
// return the hour part of the (24-hr) time of day. we do this by dividing 
// the total number of minutes by the number of minutes in an hour (60)
// as in the first function, if the input is out of range it is adjusted
int hour( int minutes_since_midnight )
{ return minutes_since_midnight % (24*60) / 60 ; }

// given an input of the number of minutes elapsed since midnight
// return the hour part of the (24-hr) time of day. we do this by dividing 
// the total number of minutes by the number of minutes in an hour (60)
// and taking the remainder of the division
int minute( int minutes_since_midnight )
{ return minutes_since_midnight % 60 ; }


A const int is an immutable int; once it has been initialised, its value can never be changed.
If we accidentally try to modify it, because of a programming error, the compiler will point out the error.
See: http://www.cplusplus.com/forum/beginner/180986/#msg888218
My proffesor doesnt want use to be using anything but the #include<iostream.h> and he only want one function in the program. He gave me some Psuedocode that he wants the program to roughly look like. How would you translate the program you made to this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

//Only one function can be used
//<iomanip> cannot be used in the program


Int main()
{
Int startHours;
Int startMins;
//ect.


getValues ( StartHours,StartMins, ect)
while(startHours !=999)// The Program is supposed to end when the user inputs 999 for the hours value.
{
getValues()
}//end main()
void getValues (int& startHours, int& startMins, ect)
{
cout<<
cin>>startHours;
}


Topic archived. No new replies allowed.