Convert 12 hours to 24 hours

I am stuck on a homework problem where I have to convert 12 hours to 24 hours.
Prompt
This programs lets you practice using the String Processing Operations. You can also use cin where the data are in compressed form. Either approach is OK.

Write a program that converts a time in 12-hour format to 24-hour format. The program will prompt the user to enter a time in HH:MM:SS AM/PM form. (The time must be entered exactly in this format all on one line.) It will then convert the time to 24 hour form. You may use a string type to read in the entire time at once, including the space before AM/PM, or you may choose to use separate variables for the hours, minutes, seconds and AM/PM.

Run your program and show the output for each of the five examples below.

Examples:

Input (12HR) Output(24hr)

5:30:00 PM 17:30:00
6:45:50 AM 06:45:50
12:32:30 AM 00:32:30
12:15:18 PM 12:15:18
1:02:22 PM 13:02:22

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main(void)
{
    int hours, minutes, seconds;
    char colon;
    string time;

    cout << "Enter a time in HH:MM:SS AM/PM: \n";
    cin >> hours >> colon >> minutes >> colon >> seconds;
    if(time=="AM")
    {
        hours=0;
    }
    else if(time=="PM")
    {
        hours += 12;
    }
    
return 0;
}  

I do not know advanced methods, so how can I make my current code better for the program to work?
Here's a start to fixing it up with 2 variable name changes to make it a bit clearer.
<fstream> not needed, <iomanip> still needed to do formatting. Maybe also include checking to make sure input meets the spec - AM or PM only, seconds < 60 etc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int hours, minutes, seconds;
    char colon{':'};
    string AMPM;
    
    cout << "Enter a time in HH:MM:SS AM/PM: \n";
    cin >> hours >> minutes >> seconds >> AMPM;
    
    if(AMPM == "PM")
    {
        hours += 12;
    }
    
    cout << hours << colon << minutes << colon << seconds << AMPM << '\n';
    
    return 0;
}
Alternatively if the input has to be a single string:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int hours, minutes, seconds;
    char colon{':'};
    string time;
    string AMPM;
    
    cout << "Enter a time in HH:MM:SS AM/PM: ";
    getline(cin, time);
    
    hours = stoi( time.substr(0,2) );
    minutes = stoi( time.substr(3,2) );
    seconds = stoi( time.substr(6,2) );
    AMPM = time.substr(9,2);
    
    if(AMPM == "PM")
    {
        hours += 12;
    }
    
    cout << hours << colon << minutes << colon << seconds << AMPM << '\n';
    
    return 0;
}
Even this way ...
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int hours;
    
    string time;
    
    cout << "Enter a time in HH:MM:SS AM/PM: ";
    getline(cin, time);
    
    hours = stoi( time.substr(0,2) );
    
    if(time.substr(9,2) == "PM")
    {
        hours += 12;
        time.replace( 0,2,to_string(hours) );
    }
    
    cout << time << '\n';
    
    return 0;
}
One small problem.....
when I input 12:32:50 AM the output is 12:32:50 when it is supposed to be 00:32:50
Not entirely sure if that output my professor provided is true but it's driving me crazy.
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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

int main(void)
{
    int hours, minutes, seconds;
    char colon;
    string AMPM;

    cout << "Enter a time in HH:MM:SS AM/PM: \n";
    cin >> hours >> colon >> minutes >> colon >> seconds >> AMPM;
    
    if(AMPM=="PM")
    {
        hours += 12;
    }
    cout << setfill('0');
    cout << setw(2) << hours << colon
         << setw(2) << minutes << colon
         << setw(2) << seconds << endl;
    
    return 0;
}
its working as you told it to.
you entered 12 am.
is am == pm?
no, do not add 12.

there are fancy ways to do it but the simple one is just to say
if its pm or its am and hours is 12
which in c++ is
if(AMPM== "PM")
hours +=12;
else
if(hours == 12) //you know it is AM here, because its not PM, from the else.
hours = 0;
Last edited on
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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main(void)
{
    int hours, minutes, seconds;
    char colon;
    string AMPM;

    cout << "Enter a time in HH:MM:SS AM/PM: \n";
    cin >> hours >> colon >> minutes >> colon >> seconds >> AMPM;
    
    if(AMPM=="PM")
    {
        hours += 12;
    }
    
    if( (AMPM=="AM") && (hours == 12))
    {
        hours = 0;
    }
    
    cout << setfill('0');
    cout << setw(2) << hours << colon
         << setw(2) << minutes << colon
         << setw(2) << seconds << AMPM << endl;
    
    return 0;
}
and its still not quite right.
what do you get for 12pm noon? 24..!
OP, can you fix that last bug on your own?

hint: if the hours are 12, and you already handled PM, a single condition will fix both cases by subtracting 12... (and any AM/12 specific code goes away or is modified into this fix).

--- advanced way ---
the simplest fix is just a %

hours%=12;
if(AMPM=="PM")
{
hours += 12;
}
//nothing after this, its all fixed up
you can even 1 line it..
hours %=12 + (AMPM==PM)*12; //this is just silly. bool is 1 or 0, so its 1*12 or 0 * 12 added.
Last edited on
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
#include <iostream>
#include <string>
#include <iomanip>

/*
 5:30:00 PM 17:30:00
 6:45:50 AM 06:45:50
 12:32:30 AM 00:32:30
 12:15:18 PM 12:15:18
 1:02:22 PM 13:02:22
 12:00:00 AM 12:00:00 NOON
 12:00:00 PM 00:00:00 MIDNIGHT
 
 */

using namespace std;

int main(void)
{
    int hours, minutes, seconds;
    char colon;
    string AMPM;
    
    cout << "Enter a time in HH:MM:SS AM/PM: ";
    cin >> hours >> colon >> minutes >> colon >> seconds >> AMPM;
    
    if(AMPM =="PM")
    {
        if(hours == 12 && minutes == 0 && seconds == 0)
        {
            hours = 0;
            AMPM = "MIDNIGHT";
        }
        else if (hours < 12)
        {
            hours += 12;
        }
        
    }
    
    if( (AMPM=="AM") && (hours == 12))
    {
        if(hours == 12 && minutes == 0 && seconds == 0)
        {
            AMPM = "NOON";
        }
        else
            hours = 0;
    }
    
    cout
    << setfill('0')
    << setw(2) << hours << colon
    << setw(2) << minutes << colon
    << setw(2) << seconds
    << '(' << AMPM << ')' << endl;
    
    return 0;
}
Topic archived. No new replies allowed.