C++ Specifications: The input should read in integer values for the hours, read
ID: 3585034 • Letter: C
Question
C++ Specifications: The input should read in integer values for the hours, read in the colon character and discard it, and eger values for the minutes. Three user defined functions should be used with the tion prototypes: getInput(int &hrs;, int min) void convertTine(int Shrs, stringampm) void displayoutput (int hrs, int min, string ampm) The function getInput should be used to store the hours and minutes input from the keyboard using reference parameters. The function convertTime) should be used to convert the hours from 24-hour format to 12-bour format and determine the AM PM information using reference parameters. The fanction displayoutput) should print out the results of the time conversion in 12-hour format using call by value parameters NOTE: The string data type is defined in the std namespace. Therefore, "using namespace std; "must be declared globally before your function prototypes since the displayOutput () function uses the string data type. Otherwise, use stdistring to refer to all string data types The setw() fanction will be needed to ensure that two spaces are always used for the minutes. To force a leading'to be printed in the case of "08" minutes, the setfill function should be used as well. Any blank spaces that are present due to the setw) function will be filled by a character passed to the setfill) function as an argument. Since we require 2 spaces to print out the minutes, and we always want to print the leading O' if necessary, setw(2) and setfill can be used to format the output just before printing the minutes to the screen. Note that printing a leading 0' for the hours iomanip> is not necessary. These functions are i anipulation library As an example, if you execute the program with the following underlined inputs, the output will be Enter a time in 24 hour formati 24:00 Time: 12:00 AM -main.o Enter a time in 24 hour formati 12:00 Time: 12:00 PM main.o Enter a time in 24 hour format: 00:08 Time: 12:08 AM -main.o Enter a time in 24 hour format: 03:09 Time: 3:09 AM Enter a time in 24 hour format: 18:50 Time: 6:50 PMExplanation / Answer
#include<iostream>
#include<string>
using namespace std;
void getInput(int &hrs, int &min);
void convertTime(int &hrs, string &m);
void displayOutput(int hrs, int min, string ampm);
int main(){
int hrs;
int min;
string ampm,
getInput(hrs, min);
cout << "Enter a time in 24 hour format: ";
cin >> hrs;
cout << ":";
cin >> min;
convertTime(hrs, ampm);
displayOutput(hrs, min, ampm);
return 0;
}
void convertTime(int &hrs, string &m){
if(hrs > 12)
{
hrs = hrs - 12;
ampm.assign("PM");
}
else if(hrs == 12){ //set AM/PM flag to PM if hour is 12 exactly, since 12 lunch time is classed as PM
ampm.assign("PM");
}
else { //if neither of above true, must be AM
ampm.assign("AM");
}
}
void displayOutput(int hrs, int min, string ampm){
if(ampm == "PM")
{
if(min < 10) { //adds leading 0 if minutes is 1-9
cout << "Time :"<<hrs << ":0" << min << " P.M.";
}
else {
cout << "Time :"<<hrs << ":" << min << " P.M.";
}
}
else
{
if(min < 10) { //adds leading 0 if minutes is 1-9
cout << "Time :"<<hrs << ":0" << min << " A.M.";
}
else {
cout << "Time :"<<hrs << ":" << min << " A.M.";
}
}
}
here is the c++ program for converting 24 hour format to 12 hour format. According to the question , i have taken input from user the time in 24 hour format via getInput function and convert it into 12 hour format via convertTime() function and finally display output via using displayOutput() function.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.