Question: #include #include / / For std::setw and std::setfill using namespace std; / / Function prototypes void displayMenu ( ) ; void input ( int& hours,

#include
#include // For std::setw and std::setfill
using namespace std;
// Function prototypes
void displayMenu();
void input(int& hours, int& minutes, int& seconds);
void output12Hour(int hours, int minutes, int seconds, char type);
void output24Hour(int hours, int minutes, int seconds);
int convertTo12Hour(int hours, char& type);
int convertTo24Hour(int hours, char type);
int main(){
int hours, minutes, seconds;
char type, answer, choice;
do {
// Display the menu and get the user's choice
displayMenu();
cin >> choice;
switch (choice){
case '1': // Convert 24-hour to 12-hour notation
input(hours, minutes, seconds);
hours = convertTo12Hour(hours, type);
output12Hour(hours, minutes, seconds, type);
break;
case '2': // Convert 12-hour to 24-hour notation
input(hours, minutes, seconds);
cout << "Enter AM or PM (A/P): ";
cin >> type;
hours = convertTo24Hour(hours, type);
output24Hour(hours, minutes, seconds);
break;
default:
cout << "Invalid choice! Please select 1 or 2."<< endl;
break;
}
cout << "Perform another conversion? (y/n): ";
cin >> answer;
} while (answer =='Y'|| answer =='y');
return 0;
}
// Function to display the menu options
void displayMenu(){
cout << "Choose the conversion type:" << endl;
cout <<"1. Convert 24-hour notation to 12-hour notation" << endl;
cout <<"2. Convert 12-hour notation to 24-hour notation" << endl;
cout << "Enter your choice (1 or 2): ";
}
// Function to get the input hours, minutes, and seconds
void input(int& hours, int& minutes, int& seconds){
cout << "Enter the hours: ";
cin >> hours;
cout << "Enter the minutes: ";
cin >> minutes;
cout << "Enter the seconds: ";
cin >> seconds;
}
// Function to display the time in 12-hour format
void output12Hour(int hours, int minutes, int seconds, char type){
cout << "The time is: "
<< setw(2)<< setfill('0')<< hours <<":"
<< setw(2)<< setfill('0')<< minutes <<":"
<< setw(2)<< setfill('0')<< seconds <<"";
if (type =='A')
cout <<"A.M."<< endl;
else
cout <<"P.M."<< endl;
}
// Function to display the time in 24-hour format
void output24Hour(int hours, int minutes, int seconds){
cout << "The time is: "
<< setw(2)<< setfill('0')<< hours <<":"
<< setw(2)<< setfill('0')<< minutes <<":"
<< setw(2)<< setfill('0')<< seconds << endl;
}
// Function to convert time from 24-hour to 12-hour notation
int convertTo12Hour(int hours, char& type){
if (hours ==0){
type ='A';
return 12; // Midnight case
} else if (hours ==12){
type ='P';
return 12; // Noon case
} else if (hours >12){
type ='P';
return hours -12; // PM case
} else {
type ='A';
return hours; // AM case
}
}
// Function to convert time from 12-hour to 24-hour notation
int convertTo24Hour(int hours, char type){
if (type =='P' && hours !=12){
return hours +12; // Convert PM to 24-hour format
} else if (type =='A' && hours ==12){
return 0; // Convert midnight case
} else {
return hours; // Return hours unchanged for AM
}
}Status: FAILED! Check: 2 Test: Successful Output II Reason: Unable to find '['The time is: 08:12:30 PM']' in the program's output. Choose the conversion type: 1. Convert 24-hour notation to 12-hour notation 2. Convert 12-hour notation to 24-hour notation Enter your choice (1 or 2): Enter the hours: Enter the minutes: Enter the seconds: Enter AM or PM (A/P): The time is: 20:12:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!