In: Computer Science
*** In C++
Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. The input is given as two integers.
This is what I have so far. The program runs for correct input values, but I cannot figure out how to accommodate for hours > 23 and minutes > 59, or negative values. My code is below:
// Writing a program that converts from 24-hour notation to 12-hour notation.//
#include
using namespace std;
void input(int&, int&, char&);
void convert(int&, int&, char&);
void output(int&, int&, char&);
int main()
{
int hours, minutes;
char ampm; //character value to determine am/pm
char again; //run the program again
//loop for re-running program
do
{
input(hours, minutes, ampm);
convert(hours, minutes,
ampm);
output(hours, minutes, ampm);
cout << endl <<
"Enter Y to run again, or any other key to exit: ";
cin >> again;
}
while (again == 'y' || again == 'Y');
return 0;
}
void input(int& hours, int& minutes, char&
ampm)
{
//loop for input and checking for invalid input
cout << "Enter time in 24hr format HH:MM
";
cin >> hours;
cin.get();
cin >> minutes;
do
{
if (hours > 23 || hours < 0)
cout << "Please enter a value between 0 and 23" <<
endl;
}
while (hours > 23);
}
void convert(int& hours, int& minutes, char&
ampm)
{
//designates PM if hour is above 12, and reduces to
12hr format
//*** try >= to reduce to two lines
if (hours > 12)
{
hours = hours - 12;
ampm = 'p';
}
else if (hours == 12) ampm = 'p';
else ampm = 'a';
}
void output(int& hours, int& minutes, char&
ampm)
{
if (ampm == 'p')
{
if (minutes < 10) cout <<
hours << ":0" << minutes << " P.M.";
else cout << hours <<
":" << minutes << " P.M.";
}
else
{
if (minutes < 10) cout <<
hours << ":0" << minutes << " A.M.";
else cout << hours <<
":" << minutes << "A.M.";
}
}
Change:
Now we are passing extra flag parameter to the input method in order to determine if valid input was provided by the user or not. Also, now we are breaking out of the do while in input method instead just showing him the message infinitely, and set the validInput flag to false. Also we should take care of invalid minutes values too, which is taken care of.
CODE:
#include <iostream>
using namespace std;
void input(int&, int&, char&,bool&);
void convert(int&, int&, char&);
void output(int&, int&, char&);
int main()
{
int hours, minutes;
char ampm; //character value to determine am/pm
char again; //run the program again
bool validInpt;//check for validity of the input
//loop for re-running program
do
{
input(hours, minutes, ampm,validInpt);
// continue with convert and ouptut only if valid input was
provided by the user
if(validInpt){
convert(hours, minutes, ampm);
output(hours, minutes, ampm);
}
cout << endl << "Enter Y to run again, or any other
key to exit: ";
cin >> again;
}
while (again == 'y' || again == 'Y');
return 0;
}
void input(int& hours, int& minutes, char&
ampm,bool& validInpt)
{
//loop for input and checking for invalid input
cout << "Enter time in 24hr format HH:MM ";
cin >> hours;
cin.get();
cin >> minutes;
validInpt=true;
do
{
if (hours > 23 || hours < 0 ||minutes<0 ||minutes>59)
{
cout << "Please enter a valid HH:MM value, HH=>00 to 23
and MM=>00 to 59" << endl;
validInpt=false;
break;
}
}
while (hours > 23);
}
void convert(int& hours, int& minutes, char&
ampm)
{
//designates PM if hour is above 12, and reduces to 12hr
format
//*** try >= to reduce to two lines
if (hours > 12)
{
hours = hours - 12;
ampm = 'p';
}
else if (hours == 12) ampm = 'p';
else ampm = 'a';
}
void output(int& hours, int& minutes, char&
ampm)
{
if (ampm == 'p')
{
if (minutes < 10) cout << hours << ":0" <<
minutes << " P.M.";
else cout << hours << ":" << minutes << "
P.M.";
}
else
{
if (minutes < 10) cout << hours << ":0" <<
minutes << " A.M.";
else cout << hours << ":" << minutes <<
"A.M.";
}
}
RESULT: