Storing outputs as a character

The program I've written is a 24-hour clock that converts inputed time in to a 12-hour clock. My issue is that I can't get the program to show whether the time converted is am or pm. It sounds simple enough but I'm restricted to doing it a certain way, because this is a school assignment. In the second function (convertTime24to12) the data going out of the function needs to consist of the hour in 12-hour format and the a.m. or p.m. stored as a char, 'a' for a.m. or 'p' for p.m. This function should not output the results to the screen, instead it should send the results back to the caller (by value or call-by-reference).
I got the time conversion part, my troubles are with storing the a.m. p.m. as characters and then calling them back out in latter functions to output "a.m." or "p.m." after the the displaying the time.

Can someone point me in the right direction because I've tried so many things and I'm left clueless as to what I need to do.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  #include <iostream>
#include <limits>

using namespace std;

void getTime24(int& hour, int& minute);
int convertTime24to12(int& hour);
void printTime12(int hour, int minute);
void printTime24(int hour, int minute);

int main() {
    
    int hour24;
    int minute;
    int hour12;
    
    getTime24(hour24, minute);
    hour12 = convertTime24to12(hour24);
    printTime12(hour12, minute);
    printTime24(hour24, minute);
    
    
    
    
    
}


void getTime24(int& hour, int& minute){
    
    char colon;
    
    cout << "Enter a time in 24 hour format (for example 13:45): ";
    cin >> hour >> colon >> minute;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    while (hour < 0 || hour > 23 || minute < 00 || minute > 59 || colon != ':') {
        cout << "That is not a valid time: Try again: ";
        cin >> hour >> colon >> minute;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    
}

int convertTime24to12(int& hour){
    

    if (hour <= 12){
        return (hour);
    }
    else
        return (hour - 12);
    
    
}

void printTime12(int hour, int minute){
    
    cout << "That time in 12 hour format is: ";
    char zero;
    if (minute < 10){
        zero = '0';
    }
    cout << hour << ":" << zero << minute << " ";
        cout << endl;
}

void printTime24(int hour, int minute){
    
    cout << "That time in 24 hour format is: ";
    char zero;
    if (minute < 10){
        zero = '0';
    }
    cout << hour << ":" << zero << minute << endl;
    
}
In the second function (convertTime24to12) the data going out of the function needs to consist of the hour in 12-hour format and the a.m. or p.m. stored as a char, 'a' for a.m. or 'p' for p.m.


The function needs to return more than one value. There are a number of ways to do this, such as using a struct or a std::pair<int, char>, or passing parameters by reference.

In fact, you are already passing the hour by reference. You just need a second parameter.
 
void convertTime24to12(int& hour, char& ap);
hour - input/output
ap - output


Or alternatively:
 
int convertTime24to12(int hour, char& ap);
hour - input
ap - output

Tutorial:
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Topic archived. No new replies allowed.