hour conversions functions

Hello,

Everything with this works except the do while in Main does not print the cout message and end program when I enter a zero, I'm pretty sure the syntax is correct. I wanted to make this program all functions within a do while loop that call one after another in sequence again over and over unless a user enters a 0. Also, I am not sure how to decipher where the best place is to declare variables when using many functions. I'd like to eventually tailor these separate functions so they can be used interchangeably in different programs that calculate different times.

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
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
using namespace std;

void validate(int& hours, int& mins);
//Function will display to user their 24-hours time if the time they enter doesn’t have hours
//less than zero or greater than 23 AND the minutes they enter is between zero and 59.
//Otherwise, function will prompt reentry until both valid hours and minutes are entered.

int subtraction(int& hours, int hourchange);
////Function will take the user entered 24-hours time hours and subtract 12 to get
////the 12-hours notation hours, thus new variable new_hours.

void swap_values(int& hours, int& new_hours);
////Function will swap out the 12-hours notation hours for the 24-hours time hours time.
////Swaps values stored in new_hours and hours.

void show_twelvehours(int& hours, int mins);
//Function will display the 12-hours notation.

void show_twentyfourhours(int new_hours, int mins);
//Function will display the 24-hours notation.

int hours = 0;
int mins = 0;
int new_hours;
int hourchange;

int main()

{
    do
    {    //function calls
        validate(hours, mins);
        new_hours = subtraction(hours, hourchange);
        swap_values(hours, new_hours);
        show_twelvehours(hours, mins);
        show_twentyfourhours(new_hours, mins);
    } while (hours != 0);
    cout << "Zero ends the program" << endl;
}

//Function Definitions
void validate(int& hours, int& mins)
{
    cout << "Enter a number for hours and another number for minutes." << endl;
    cin >> hours;
    cin >> mins;

    while (hours < 0 || hours > 23)
    {
        cout << "Reenter hours." << endl;
        cin >> hours;
    }

    while (mins < 0 || mins > 59)
    {
        cout << "Reenter minutes." << endl;
        cin >> mins;
    }
}

int subtraction(int& hours, int hourchange)
{   
    int hourchange = 12;
    new_hours = hours - hourchange;
    return new_hours;
}

void swap_values(int& hours, int& new_hours)
{
    int temp;
    temp = hours;
    hours = new_hours;
    new_hours = temp;
}

void show_twelvehours(int& hours, int mins)
{   
    if (mins < 10)
        cout << "Your 12-hour time is " << hours << ":0" << mins << endl;
    else
        cout << "Your 12-hour time is " << hours << ":" << mins << endl;
}

void show_twentyfourhours(int new_hours, int mins)
{   
    if (mins < 10)
        cout << "Converted back to 24-hour time is " << new_hours << ":0" << mins << endl;
    else
        cout << "Converted back to 24-hour time is " << new_hours << ":" << mins << endl;
}
Did you also enter a value for the minutes? The way you have your program structured you will call all the following functions before the program tries to exit, so if any of the following functions changes hours the program may not exit. You should be careful about passing hours and minutes by reference into your functions unless you actually mean to change the values inside the functions.

closed account (SECMoG1T)
well i dint look at the details of the program but if you want the do while to print the message you'v got move it into the do after this line
show_twentyfourhours(new_hours, mins);
Last edited on
Thank you, the program won't calculate the time changes unless I use the call by references for these functions:

void validate(int& hours, int& mins)
void swap_values(int& hours, int& new_hours)

Is there a better way for me to have implemented these functions so I wouldn't have to use the call by references? And what would be bad about changing the values inside the functions?

I see what you mean about calling the functions before the program would exit so I edited that.
Topic archived. No new replies allowed.