Question

in C++ Write a program that converts a time in 12-hour format to 24-hour format. The...

in C++

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;

//function to convert 12 hours format to 24 hours format
void format12HoursTo24Hours(string str)
{
//if time is in AM
   if ((str[9] == 'A') || (str[8] == 'A'))
   {
       for (int i=0; i <= 7; i++)
       {
           cout << str[i];
       }
   }
   //if time is in PM
   else
   {
   int hh;
   if(((int)str[1] - '0') >=0 && ((int)str[1] - '0') <=9)
   {
   hh = (((int)str[0] - '0') * 10 + ((int)str[1] - '0'));
   }
   else
   {
   hh = ((int)str[0] - '0');
   }
  
       if (hh == 12)
       {
           for (int i=0; i <= 7; i++)
               cout << str[i];
       }
       else
       {
           hh = hh + 12;
           cout << hh;
           if(str[2]!=':')
           cout<<":";
           for (int i=2; i <= 7; i++)
               cout << str[i];
       }
   }
}

int main()
{
//string variable declartion
string timeStr;
  
//display message
cout<<"Enter time in HH:MM:SS AM/PM format: ";
cin>>timeStr;
  
cout<<endl<<"The time in 24 hours format is: ";
  
//function calling
format12HoursTo24Hours(timeStr);
return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
in C++ Write a program that converts a time in 12-hour format to 24-hour format. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it...

    C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion, and two for output (one for 12-hour time and another for 24-hour time). Record the AM/PM information as a value of type char, ‘A’ for AM and ‘P’ for PM. Thus, the function for doing the...

  • Can someone solve it with C plz Write a program that inputs a time from the...

    Can someone solve it with C plz Write a program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits, for example, “1:10 AM” or “11:30 PM”. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four digit military time based on a 24 hour clock. For example, “1:10 AM” would...

  • Time Displacement (12-hour format) Write a program that requests the current time and a waiting time...

    Time Displacement (12-hour format) Write a program that requests the current time and a waiting time as two integers for the number of hours and the number of minutes to wait. The program then outputs what the time will be after the waiting period. Use 24-hour notation for the input time and 12-hour notation for the output time. Include a loop that lets the user repeat this calculation for additional input values until the user says she or he wants...

  • 1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice...

    1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the...

  • Create the Code using C Program: 2. Write a program to convert the time from 24-hour...

    Create the Code using C Program: 2. Write a program to convert the time from 24-hour notation to 12-hour notation and vice-versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following function: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the...

  • Need help creating a basic java string program using nested if/else, return loops or while loops...

    Need help creating a basic java string program using nested if/else, return loops or while loops or charAt methods while returning information using a console, Please leave notes as to compare my program and see where I went wrong or could've used a different method. secondsAfterMidnight Input: String that represents time of day Returns: integer number of seconds after midnight (return -1 if String is not valid time of day) General time of day format HH:MM:SS(AM/PM) These are examples where...

  • Write the python function time to midnight() that takes the name of a file containing multiple...

    Write the python function time to midnight() that takes the name of a file containing multiple lines of text giving time in HH:MM:SS HRS or HH:MM:SS AM or HH:MM:SS PM format one per line. The HRS notation indicates CSE 101 – Summer 2019 Lab Assignment #9 2 that the time has been given in 24-hour time format. Each line tells the current time of the day. The function reads the file line by line and calculates the number of seconds...

  • Determine the time period of the day in C++ a. Request an hour in military time...

    Determine the time period of the day in C++ a. Request an hour in military time between 0 and 23 inclusive. b. If the value for hour is invalid: 1. Output Invalid hour". 2. Repeat the input request. a. Request the minutes b. Print the time using hh:mm format C. Convert to 12 hour format (i.e. AM/PM) and print Use a single if/else-if/else structure to do the following: 1. If the hour is between 0 and 12 inclusive, output "It...

  • I need help to write a code in python to do the following: I need to...

    I need help to write a code in python to do the following: I need to extract only the start hour from each row in the the column below into a separate column. This is a part of database and the School_Hours column is having the following info: School_Hours 08:00 AM-03:00 PM 08:15 AM-03:15 PM 08:30 AM-03:30 PM 7:45 AM - 2:45 PM 7:30 AM-2:30 PM 7:45 AM - 2:45 PM 8:30 AM - 2:55 PM 08:30 AM-03:30 PM 8:30...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT