Question

In: Computer Science

*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example,...

*** 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.";
   }
}

Solutions

Expert Solution

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:


Related Solutions

In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume...
In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter the time as a 4-digit number with no colon. Define an exception class called InvalidTimeFormatException. If the user enters an invalid time lime 1065 or 2515, the program should throw and handle an InvalidTimeFormatException. NOTE: Assume the user will enter the time as a 4-digit number with no colon. SAMPLE OUTPUT: Enter time in 24-hour notation: 1614 That is the...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
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;...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
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;...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
Write a C++ program that converts an infix expression, which includes (, ), +, -, *,...
Write a C++ program that converts an infix expression, which includes (, ), +, -, *, and / operations to postfix notation. The program should allow the user to enter an infix expression using lower case characters, then it will display the result of conversion on the screen. (Note: Use the STL stack class to accomplish the solution.).
Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and...
Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and 24-hour time (military time). We will assume in the context there is no need for seconds, so time12 use only hours (from 1 to 12), minutes, and an "a.m." or "p.m." designation. Our time24 class, which uses hours (from 00 to 23), minutes and seconds. 12-Hours Time 24-Hours Time 12:00 a.m. (midnight) 00:00 12:01 a.m. 00:01 12:00 p.m. 12:00 6:00 p.m. 18:00 11:59 p.m....
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
2. Write a C++ program that; Takes in the weight of a person in Kilograms, converts...
2. Write a C++ program that; Takes in the weight of a person in Kilograms, converts and outputs the equivalent weight in pounds. Format your output to 3 decimal places. Your output should look like this 53.000 Kg is equivalent to 123.459 Ibs (Note 1Kg = 2.2046226218488 lbs) Takes in the price of an item on an online store in pound sterling, converts and outputs the equivalent price in U.S dollars. Format your output to 2 decimal places. Your output...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT