menu
bartleby
search
close search
Hit Return to see all results

Write a program that converts a time in 12-hour format to 24-hour format. The program will prompt the userto 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 onceincluding the space 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 PM17:30:006:45:50 AM06:45:5012:32:30 AM00:32:3012:15:1812:15:18 PM1:02:22 PM13:02:22

Question

C++ programming

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 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
12:15:18 PM
1:02:22 PM
13:02:22
help_outline

Image Transcriptionclose

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 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 12:15:18 PM 1:02:22 PM 13:02:22

fullscreen
check_circleAnswer
Step 1

Program Algorithm

  • Declare the variable “s” of string type to take the input in 12-Hour Format.
  • Call the function “_24_Hour_Format(s)” in main ().
  • Define the function “_24_Hour_Format (string s)” where the calculation of hours will be executed.
  • Check if the input time is in AM or PM.
  • If time is in AM, then the if statements will be executed.
  • Otherwise else statements will be executed.

 

Step 2

Source Code

#include<iostream>

using namespace std;

void _24_Hour_Format(string s)

{

    // calculation of hours

    int hour1 = (int)s[1] - '0';

    int hour2 = (int)s[0] - '0';

    int hour = (hour2 * 10 + hour1 % 10);

    if (s[8] == 'A') // for the time in "AM"

    {

        if (hour == 12)

        {

            cout << "00";

            for (int i=2; i <= 7; i++)

                cout << s[i];

        }

        else

        {

            for (int i=0; i <= 7; i++)

                cout << s[i];

        }

    }

    // for the time in "PM"

    else

    {

        if (hour == 12)

     &...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Our solutions are written by experts, many with advanced degrees, and available 24/7

See Solution
Tagged in

Engineering

Computer Science

Object Oriented Programming

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: define virtual memory

A: Virtual memory, in context with computing, is memory management technique that is used by the operat...

question_answer

Q: Find a regular grammar that generates the language on Σ = {a, b} consisting of all strings with no m...

A: Regular grammar for the language Σ = {a, b} is as follows:

question_answer

Q: (a) Find an nfa with three states that accepts the language L = {an : n ≥ 1} ∪ {bmak : m ≥ 0, k ≥ 0}

A: Graphical representation of NFA:The starting state is denoted by an empty incoming arc, the vertices...

question_answer

Q: How do I get values from a dictionary into a Python code when my values are in a .txt file?

A: Program instructions:Initialize a dictionary variable named "dictionary" and use open() function to ...

question_answer

Q: Write a function is_mirror(s) that takes as input a string s and returns True if s is a mirrored str...

A: The algorithm for the program would be as follows:Find the length of the program if it is even lengt...

question_answer

Q: Using adders, subtracters, comparers, copiers, or multipliers, draw block diagrams for Turing machin...

A: Solution:Turing machine:The Turing machine is theoretical computing machine that can be used to iden...

question_answer

Q: how can I write a statement in C++ that would print last name, followed by a comma, followed by firs...

A: Given below is the C++ program to print a statement with last name, followed by comma, followed by f...

question_answer

Q: Hello! I am trying to write a program in Pynthon. Need help. Requirements: Write a .py file like Kev...

A: Program Plan:Import required packagesPrompt the path and the number of characters to delete from the...

question_answer

Q: What language is accepted by the Turing machine whose transition graph is in the figure below?

A: Turing machine:Turing machine is a model of a hypothetical computing machine which can use a predefi...

Sorry about that. What wasn’t helpful?