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 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 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
Write a program that converts a given floating point binary number with a 24-bit normalized mantissa...
Write a program that converts a given floating point binary number with a 24-bit normalized mantissa and an 8-bit exponent to its decimal (i.e. base 10) equivalent. For the mantissa, use the representation that has a hidden bit, and for the exponent use a bias of 127 instead of a sign bit. Of course, you need to take care of negative numbers in the mantissa also. Use your program to answer the following questions: (a) Mantissa: 11110010 11000101 01101010, exponent:...
Instructions Write a program in C# that converts a temperature given in Fahrenheit to Celsius. Allow...
Instructions Write a program in C# that converts a temperature given in Fahrenheit to Celsius. Allow the user to enter values for the original Fahrenheit value. Display the original temperature and the formatted converted value. Use appropriate value returning methods for entering (input), calculating, and outputting results.
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts a Celsius temperature to Fahrenheit. Modify that program so it uses a loop to display a table of the Celsius temperatures 0–20, and their Fahrenheit equivalents. Display table on screen and it a .txt file. c++
In a c programming Write a program that converts upper case letters to lower case letters...
In a c programming Write a program that converts upper case letters to lower case letters or vice versa: Enter a sentence: What a GREAT movie is! Converted sentence: wHAT_A_great_MOVIE_IS_ Convert all non-alphabetical letters to ‘_’
Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT