Time coverter program

I am writing a program that converts military time into 12 hour time. I am using a switch to do the converting but I am getting a strange error.

invalid digit "8" in octal constant|

error: invalid digit "9" in octal constant|

I don't know what this means. I think the logic is solid but I cant seem to make the code work. Any help would be great!

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    //int x = 5;
    //cout << setw(2) << setfill('0') << x;
    int hours, mins,secs;
    cout<<"Enter a time value in the following format: HH MM SS\n";
    cin>>hours>>mins>>secs;
    if(hours>24)
    {
        cout<<"Hour must be between 0 and 23 inclusive.";
    }
    switch(hours)
    {
    case 00:
        cout<<"12:"<<mins<<":"<<secs<<"AM\n";
        break;

    case 01:
         cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
         break;

    case 02:
         cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
         break;

    case 03:
         cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
         break;

    case 04:
         cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
         break;

    case 05:
         cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
         break;

    case 06:
          cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
         break;

    case 07:
         cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
         break;

    case 08:
         cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
         break;

    case 09:
         cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
         break;

    case 10:
        cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
        break;

    case 11:
        cout<<hours<<":"<<mins<<":"<<secs<<"AM\n";
        break;

    case 12:
        cout<<hours<<":"<<mins<<":"<<secs<<"PM\n";
        break;

    case 13:
        cout<<"01:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 14:
        cout<<"02:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 15:
        cout<<"03:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 16:
        cout<<"04:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 17:
        cout<<"05:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 18:
        cout<<"06:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 19:
        cout<<"07:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 20:
        cout<<"08:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 21:
        cout<<"09:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 22:
        cout<<"10:"<<mins<<":"<<secs<<"PM\n";
        break;

    case 23:
        cout<<"11:"<<mins<<":"<<secs<<"PM\n";
        break;
    }
}
A leading 0 in an integer means an octal number - so 8 & 9 are invalid.

HTH
So that means I cant use the hours variable because it has a leading zero?
Yes, you might have to come with another way of dealing with the input.

The easiest way would be to have the input as ordinary int without the leading zero, then do math on that number.

If the leading zero has to be there, then make the variable a string, determine whether the zero is there and remove it, then convert the rest to a number.
You seem to not be making use of any of the stream manipulators in iomanip
http://ideone.com/4lli41
Topic archived. No new replies allowed.