Question

In: Computer Science

home / study / engineering / computer science / computer science questions and answers / instructions...

home / study / engineering / computer science / computer science questions and answers / instructions write a program to convert the time from 24-hour notation to 12-hour notation ...

Question: Instructions Write a program to convert the time from 24-hour notation to 12-hour notation and vi...

Instructions

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 input;
  • and function(s) to display the results.
    (For 12-hour time notation, your program must display AM or PM.)
  • the language must be c++, also the out put format should be (The time is: hh:mm:ss

Solutions

Expert Solution

(Please do rate the answer if you found useful - read comments for explanation - output is also attached )

Program Code:

#include <iostream>
#include <string>
using namespace std;

string inputstr() {
string st;
cout<<"Enter Time in hh:mm:ss AM or hh:mm:ss PM format:\n";
cin>>st;
return st;
}
string inputstr2() {
string st;
cout<<"Enter Time in hh:mm:ss :\n";
cin>>st;
return st;
}

int twentyfour(string str)
{
// compute hours
int hour1 = (int)str[1] - '0';
int hour2 = (int)str[0] - '0';
int hh = (hour2 * 10 + hour1 % 10);
return hh;
  

}

int twelve(string str) {
// compute hours
int hour1 = (int)str[0] - '0';
int hour2 = (int)str[1] - '0';
  
int hh = hour1 * 10 + hour2;
return hh;
  
}

void disp1(int hh,string str) {
// code block to convert time in AM to 24hrs
if (str[8] == 'A')
{
if (hh == 12)
{
cout << "00";
for (int i=2; i <= 7; i++)
cout << str[i];
}
else
{
for (int i=0; i <= 7; i++)
cout << str[i];
}
}
  
// Code block to convert time in PM to 24hrs
else
{
if (hh == 12)
{
cout << "12";
for (int i=2; i <= 7; i++)
cout << str[i];
}
else
{
hh = hh + 12;
cout << hh;
for (int i=2; i <= 7; i++)
cout << str[i];
}
}
cout<<"\n";
  
}


void disp2(int hh,string str) {
// Finding/Determining whether time is in AM or PM
string Mr;
if (hh < 12) {
Mr = "AM";
}
else
Mr = "PM";
  
hh %= 12;
  
// Handle 00 and 12 case separately
if (hh == 0) {
cout << "12";
  
// Display minutes and seconds
for (int i = 2; i < 8; ++i) {
cout << str[i];
}
}
else {
cout << hh;
// Display minutes and seconds
for (int i = 2; i < 8; ++i) {
cout << str[i];
}
}
  
//Used to print AM or PM after Time
cout << " " << Mr << '\n';
}   


int main()
{ string str;
int ch;
int hh;
while(1) {
cout<<"\n1: 12-hour notation to 24-hour notation\n";
cout<<"2: 24-hour notation to 12-hour notation\n";
cout<<"3: Exit\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch) {
case 1: str=inputstr();
hh= twentyfour(str);
disp1(hh,str);
break;
case 2:str=inputstr2();
hh=twelve(str);
disp2(hh,str);
break;
case 3: exit(0);break;
default: cout<<"Wrong Choice";
}
}
return 0;
}

Output:



Related Solutions

home / study / engineering / computer science / computer science questions and answers / create...
home / study / engineering / computer science / computer science questions and answers / create a new java file, containing this code public class datastatsuser { public static void ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: Create a new Java file, containing this code public class DataStatsUser { public static void... Create a new Java file, containing this code public class DataStatsUser { public static void main...
home / study / engineering / computer science / computer science questions and answers / Modify...
home / study / engineering / computer science / computer science questions and answers / Modify StudentLinkedList Class By Adding The Following Methods:  PrintStudentList: Print ... Your question has expired and been refunded. We were unable to find a Chegg Expert to answer your question. Question: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by call... Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and printing “toString” of every object in...
home / study / engineering / computer science / computer science questions and answers / 2....
home / study / engineering / computer science / computer science questions and answers / 2. design an er-diagram for a bank that implements the following requirements. the database ... Question: 2. Design an ER-diagram for a bank that implements the following requirements. The database you d... 2. Design an ER-diagram for a bank that implements the following requirements. The database you design should store information about customers, accounts, branches and employees • Customer: Customers are identified by their SSN....
home / study / engineering / computer science / computer science questions and answers / write...
home / study / engineering / computer science / computer science questions and answers / write a program that in c++: 1.prompts the user to enter a positive integer, think of this ... Question: Write a program that in C++: 1.Prompts the user to enter a positive integer, think of this intege... Write a program that in C++: 1.Prompts the user to enter a positive integer, think of this integer as representing a specific number of pennies. 2. The program...
home / study / engineering / computer science / computer science questions and answers / Using...
home / study / engineering / computer science / computer science questions and answers / Using JAVA The Following Code Is Able To Read Integers From A File That Is Called "start.ppm" ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: Using JAVA The following code is able to read integers from a file that is called "start.ppm" ont... Using JAVA The following code is able to read integers from...
home / study / engineering / computer science / questions and answers / this is c....
home / study / engineering / computer science / questions and answers / this is c. create three files to submit. contacts.h ... Question: This is C. Create three files to submit. Contacts.... Bookmark This is C. Create three files to submit. Contacts.h - Struct definition, including the data members and related function declarations Contacts.c - Related function definitions main.c - main() function (2) Build the ContactNode struct per the following specifications: Data members char contactName[50] char contactPhoneNum[50] struct ContactNode*...
home / study / engineering / computer science / questions and answers / working with layout...
home / study / engineering / computer science / questions and answers / working with layout managers. notes: 1. in part ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: Working with Layout Managers. Notes: 1. In part 2,... Bookmark Working with Layout Managers. Notes: 1. In part 2, note that the Game class inherits from JPanel. Therefore, the panel you are asked to add to the center of the...
home / study / engineering / computer science / questions and answers / i have a...
home / study / engineering / computer science / questions and answers / i have a c++ question, its already posted on here ... Question: I have a c++ question, its already posted on here ... Bookmark I have a c++ question, its already posted on here but the answer given is way too complex and i dont understand it... its only the first month of c++ so please use the basic code... thank you. Assume that ax^2 + bx...
The questions read as follows: home / study / engineering / computer science / computer science...
The questions read as follows: home / study / engineering / computer science / computer science questions and answers / Course Grades Java Class In A Course, A Teacher Gives The Following Tests And Assignments: ... Question: Course grades java class In a course, a teacher gives the following tests and assignments: A lab ... course grades java class In a course, a teacher gives the following tests and assignments: A lab activity that is observed by the teacher and...
home / study / science / nursing / nursing questions and answers / This Is A...
home / study / science / nursing / nursing questions and answers / This Is A Theoretical Case Taken From VHA Intensive Ethics Advisory Committee Training, 1998, ... Your question has been posted. We'll notify you when a Chegg Expert has answered. Post another question. Next time just snap a photo of your problem. No typing, no scanning, no explanation required. Get Chegg Study App Question: This is a theoretical case taken from VHA Intensive Ethics Advisory Committee Training, 1998,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT