military change to normal time?

it does not give me the right time if i do 00:55

this is what it gives me
{
Type in time to convert
from military 24 hour time to AM-PM
Hours in the range of 0 to 24
Minutes in the range of 0 to 59
Example type in 18:30 hours to give 6:30 PM
00:55
Military time = 00:55
Standard time = 0:55 AM// needs to be 12:55 time

RUN SUCCESSFUL (total time: 7s)
}

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
  using namespace std;

//No Global Constants

//Function Prototypes
void input(int &,int &);
char cnvsn(int &);
void output(int,int,char);

int main(int argc, char** argv) {
    //Declare the variables
    int hours,minutes;
    //Input the hours and minutes
    input(hours,minutes);
    //Convert the hours and return Morning or Afternoon
    char ampm=cnvsn(hours);
    //Output the time
    output(hours,minutes,ampm);
    //Exit stage right
    return 0;
}

void output(int h,int m,char ap){
    //Display the results
    if(ap=='P'){
        cout<<"Military time = "<<h+12;
    }else{
        cout<<"Military time = ";
        if(h<10)cout<<"0";
        cout<<h;
    }
    cout<<":";
    if(m<10)cout<<"0";
    cout<<m<<endl;
    cout<<"Standard time = "<<h<<":";
    if(m<10)cout<<"0";
    cout<<m<<" "<<ap<<"M"<<endl;
}

char cnvsn(int &h){
    char pa='A';
    if(h>12){
        pa='P';
        h-=12;
    }
    return pa;
}

void input(int &h,int &m){
    //Prompt the user for inputs
    char colon;
    do{
        cout<<"Type in time to convert"<<endl;
        cout<<"from military 24 hour time to AM-PM"<<endl;
        cout<<"Hours in the range of 0 to 24"<<endl;
        cout<<"Minutes in the range of 0 to 59"<<endl;
        cout<<"Example type in 18:30 hours to give 6:30 PM"<<endl;
        //Read in the hour
        //Declare and Read in the colon
        //Read in the minutes
        cin>>setw(2)>>h>>colon>>setw(2)>>m;
    }while(h>24||m>59||h<0||m<0);
}
Trace what is happening

Lets follow hours if hours = 0

h = 0
input(hours, ....)
h still 0

cnvsn(hours)
is 0 > 12 no
h still 0

did we get 'P' no
h still 0

cout h still a 0
cout h still a 0

You could put a check in there , something like if h is 0 at this point h really should be 12.

Topic archived. No new replies allowed.