Question

Write a program that inputs a time from the console. The time should be in the...

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 then convert the time into a four digit military time based on a 24 hour clock. For example, “1:10 AM” would output “0110 hours” and “12:30 PM” would output “2330 hours”.

c++

Homework Answers

Answer #1

Code:

#include<iostream>
#include<string.h>
#include<cstdlib>
using namespace std;

int main()
{
string t;
cout<<"Enter time in (HH:MM AM) or (HH:MM PM) format:";
getline(cin,t,'n');

int col=t.find(":");
int spc=t.find(" ");

string h=t.substr(0,col);
string m=t.substr(col+1,spc-col-1);
string mer=t.substr(spc+1);

int hrs=atoi(h.c_str());

if(mer=="PM" && hrs!=12)
hrs=hrs+12;
else if(mer=="AM" && hrs==12)
hrs=0;

if(hrs>=0 && hrs<=9)
h="0";
else
h="";

char H[5];
h+=itoa(hrs,H,10);

result=h+m+"hours";
cout<<result;
return 0;
}

Know the answer?
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
Similar Questions
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM. (A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below: void input(int& hours24, int& minutes); //Precondition: input(hours, minutes) is called with //arguments capable of being assigned values. //Postcondition: // user is prompted for time in 24 hour format: // HH:MM, where...
1. Complete a class Clock that represents time on a 24-hour clock, such as 00:00, 15:30,...
1. Complete a class Clock that represents time on a 24-hour clock, such as 00:00, 15:30, or 23:59 ○ Time is measured in hours (00 – 23) and minutes (00 – 59) ○ Times are ordered from 00:00 (earliest) to 23:59 (latest) Complete the first constructor of the class Clock ● It takes two arguments: h and m and creates a new clock object whose initial time is h hours and m minutes ● Test cases: Clock clock1 = new...
In SQL working with data types 1. Write a SELECT statement that returns these columns from...
In SQL working with data types 1. Write a SELECT statement that returns these columns from the Instructors table: a. The monthly salary (the AnnualSalary column divided by 12) b. A column that uses the CAST function to return the monthly salary with 1 digit to the right of the decimal point c. A column that uses the CONVERT function to return the monthly salary as an integer d. A column that uses the CAST function to return the monthly...
In each of the projects that follow, you should write a program that contains an introductory...
In each of the projects that follow, you should write a program that contains an introductory docstring. This documentation should describe what the program will do (analysis) and how it will do it (design the program in the form of a pseudocode algorithm). Include suitable prompts for all inputs, and label all outputs appropri- ately. After you have coded a program, be sure to test it with a reasonable set of legitimate inputs. 1 The tax calculator program of the...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
Write a Node.js program that outputs a random square to the console up to 10. basically,...
Write a Node.js program that outputs a random square to the console up to 10. basically, create a quick function to get a random number and then display a asterisk length and width of that random number I use my terminal to run the javascript file For example, if the random number is 6, then the output would be a 6x6 box of " * ": * * * * * * * * * * * * * *...
In each of the projects that follow, you should write a program that contains an introductory...
In each of the projects that follow, you should write a program that contains an introductory docstring. This documentation should describe what the program will do (analysis) and how it will do it (design the program in the form of a pseudocode algorithm). Include suitable prompts for all inputs, and label all outputs appropri- ately. After you have coded a program, be sure to test it with a reasonable set of legitimate inputs. 1 The tax calculator program of the...
How to write a C++ program. Additive persistence is a property of the sum of the...
How to write a C++ program. Additive persistence is a property of the sum of the digits of an integer. The sum of the digits is found, and then the summation of digits is performed creating a new sum. This process repeats until a single integer digit is reached. Consider the following example: 1. The beginning integer is 1234 2. Sum its digits is 1+2+3+4 = 10 3. The integer is now 10 4. The sum of its digits is...
In C language Write a program that: simulates a time clock does nested for loops with...
In C language Write a program that: simulates a time clock does nested for loops with one switch statement. Declares variables hours, minutes, seconds as integers.  For hours are zero to &lt; 24 however switch when hours are greater than 24 print on newline “25error” switch when hours are thirteen break switches otherwise by default print on new line “top of the hour!” , For minutes are zero to 60, For seconds are zero to 60 in this loop print on...
In this lab, you will write a program that will read a canonical phone number form...
In this lab, you will write a program that will read a canonical phone number form the user, then prints it back to the properly formatted in the for “xxx-xxxx”. You program must achieve the following requirements: 1. If the user enters the number “0”, the program should exit immediately. 2. If the user enters any phone number that is grater than or less than 7-digits, the program should report an error back. In other words, only valid 7-digit numbers...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT