Could please edit my code ? The code can compile case \'1\' successfly but I cou
ID: 3680478 • Letter: C
Question
Could please edit my code ?
The code can compile case '1' successfly but I could't get get case '2'
#include <iostream>
using namespace std;
// prototype functions
void input24(int& hours24, int& minutes24);
bool input12(int& hours12, int& minutes12, bool& isAM);
void output(int hours, int minutes, char type);
int from24To12(int hours, char& type);
int from12To24(int& hours, bool& isAM);
int main() {
//declaring variables of numerous types
int hours, minutes;
char type, answer;
bool isAM ;
cout << "Please selects the conversion you want to perform: ";
cout << "1 - to convert from 24-hour format to 12-hour format. 2 - to convert from 12-hour format to 24-hour format. ";
cin >> answer;
// directory based on user's selection
switch (answer)
{
case '1':
input24(hours, minutes);
hours = from24To12(hours, type);
output(hours, minutes, type);
break;
case '2':
input12(hours, minutes, isAM);
hours = from12To24(hours, isAM);
output(hours, minutes, type);
break;
}
}
// prototype function to take user input for 24-our format
void input24(int& hours24, int& minutes24) {
cout << "Enter the hours for the 24-hour format: ";
cin >> hours24;
cout << "Enter the minutes for the 24-hour format : ";
cin >> minutes24;
}
// logical function to return day time or evening time
bool input12(int& hours12, int& minutes12, bool& isAM)
{
char c;
cout << "Enter the hours for the 12-hour format : ";
cin >> hours12;
cout << "Enter the hours for the 12-hour format : ";;
cin >> minutes12;
cout << "Enter(D) for daytime and (N) for evening time ";
cin >> c;
if (c == 'D' || c == 'd')
isAM = true;
else
isAM = false;
return (isAM);
}
// function to output the time in 12-hour format.
void output(int hours, int minutes, char type) {
cout << "The time converted to 12 hour format is: " << hours << ":";
cout.width(2);
cout.fill('0');
cout << minutes;
cout << ((type == 'A') ? " A.M." : " P.M.") << endl;
}
int from24To12(int hours, char& type) {
type = (hours < 12) ? 'A' : 'P';
if (hours == 0) hours = 12; // conditional statement to chech for entry validity
return (hours < 13) ? hours : hours - 12;
}
int from12To24(int& hours, bool& isAM)
{
if (!isAM) // conditional statement to check for day/evening time
hours = 12 + hours;
return(hours); // retums variable "hours" as integer value
}
Explanation / Answer
Modification of code in 46th line change "Enter the Hours" should change it as
Enter the Minutes in 46th line
i.e cout << "Enter the Minutes for the 12-hour format : ";
The code is succesfully working in both the cases
i.e
1.converting 24 hour format to 12 hour format
2.converting 12 hour format to 24 hour format
Solution.cpp
#include <iostream>
using namespace std;
// prototype functions
void input24(int& hours24, int& minutes24);
bool input12(int& hours12, int& minutes12, bool& isAM);
void output(int hours, int minutes, char type);
int from24To12(int hours, char& type);
int from12To24(int& hours, bool& isAM);
int main() {
//declaring variables of numerous types
int hours, minutes;
char type, answer;
bool isAM ;
cout << "Please selects the conversion you want to perform: ";
cout << "1 - to convert from 24-hour format to 12-hour format. 2 - to convert from 12-hour format to 24-hour format. ";
cin >> answer;
// directory based on user's selection
switch (answer)
{
case '1':
input24(hours, minutes);
hours = from24To12(hours, type);
output(hours, minutes, type);
break;
case '2':
input12(hours, minutes, isAM);
hours = from12To24(hours, isAM);
output(hours, minutes, type);
break;
}
}
// prototype function to take user input for 24-our format
void input24(int& hours24, int& minutes24) {
cout << "Enter the hours for the 24-hour format: ";
cin >> hours24;
cout << "Enter the minutes for the 24-hour format : ";
cin >> minutes24;
}
// logical function to return day time or evening time
bool input12(int& hours12, int& minutes12, bool& isAM)
{
char c;
cout << "Enter the hours for the 12-hour format : ";
cin >> hours12;
cout << "Enter the Minutes for the 12-hour format : ";;
cin >> minutes12;
cout << "Enter(D) for daytime and (N) for evening time ";
cin >> c;
if (c == 'D' || c == 'd')
isAM = true;
else
isAM = false;
return (isAM);
}
// function to output the time in 12-hour format.
void output(int hours, int minutes, char type) {
cout << "The time converted to 12 hour format is: " << hours << ":";
cout.width(2);
cout.fill('0');
cout << minutes;
cout << ((type == 'A') ? " A.M." : " P.M.") << endl;
}
int from24To12(int hours, char& type) {
type = (hours < 12) ? 'A' : 'P';
if (hours == 0) hours = 12; // conditional statement to chech for entry validity
return (hours < 13) ? hours : hours - 12;
}
int from12To24(int& hours, bool& isAM)
{
if (!isAM) // conditional statement to check for day/evening time
hours = 12 + hours;
return(hours); // retums variable "hours" as integer value
}
output
sh-4.3$ g++ -o main *.cpp
sh-4.3$ main
Please selects the conversion you want to perform:
1 - to convert from 24-hour format to 12-hour format.
2 - to convert from 12-hour format to 24-hour format.
2
Enter the hours for the 12-hour format : 3
Enter the Minutes for the 12-hour format : 12
Enter(D) for daytime and (N) for evening time
N
The time converted to 12 hour format is: 15:12 P.M.
Enter the hours for the 24-hour format: 13
Enter the minutes for the 24-hour format : 12
The time converted to 12 hour format is: 1:12 P.M