32767:0 Why?

I have written this program to convert military time into standard AM/PM time, however the output I get is always the same. It's 32767:0

If I omit hours2 from the void output function then it gets rid of 32767. Also if I get rid of minutes it gets rid of the zero, so I know those are connected for sure. I can't guess why this is happening. Any suggestions?


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
#include <iostream>
void input(int hours, int minutes);
void output(int hours2, int minutes, std::string thing);
void compute(int hours, int minutes, std::string thing, int hours2);
using namespace std;
int main()
{
    std::string thing;
    int hours, minutes, hours2;
    input(hours, minutes);
    compute(hours, minutes, thing, hours2);
    output(hours2, minutes, thing);
    
    return 0;
}

void input(int hours, int minutes)
{
    cout << "Enter the time in hours." << endl;
    cin >> hours;
    cout << "Enter the time in minutes." << endl;
    cin >> minutes;
}

void compute(int hours, int minutes, std::string thing, int hours2)
{
    if (hours > 12)
        hours2 = hours - 12;
    else
        hours2 = hours;
    if (hours > 11)
        thing = "PM";
    else
        thing = "AM";
}

void output(int hours2, int minutes, std::string thing)
{
    cout << hours2 << ":" << minutes << " " << thing;
}
All your variables are pass by value try passing them by reference. Take a look at this thread.

http://www.cplusplus.com/forum/beginner/115615/
Last edited on
Pass by reference, so that changes to hours, minutes and things are preserved after function returns:
1
2
3
void input(int & hours, int & minutes);
void output(int & hours2, int & minutes, std::string & thing);
void compute(int & hours, int & minutes, std::string & thing, int & hours2);


http://www.cplusplus.com/doc/tutorial/functions2/
void output(int & hours2, int & minutes, std::string & thing);

Output doesn't really need to have reference parameters passed to it. The function isn't intended to change the data.
Topic archived. No new replies allowed.