troubleshooting Question

How do I write a C++ program that converts from 24-hour notation to 12-hour notation?

Avatar of parvenu51
parvenu51 asked on
13 Comments4 Solutions5831 ViewsLast Modified:
I need help in writing a C++ program that converts 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. There are three functions: one for input, one to do the conversion, and one for output. Record the A.M./P.M. information as a value of type char, 'A' for A.M. and 'P' for P.M. Thus, the function for doing the conversions will have a call-by-reference formal parameter of type char to record whether it is A.M. of P.M. ) Specifically, I need help iin defining the function :  int convertTo12Hour(int hours, char& type)
The code I have so far is:

#include <iostream>
using namespace std;

void input(int& hours, int& minutes);
void output(int hours, int minutes, char type);
int convertTo12Hour(int hours, char& type);

int main() {
  int hours;
  int minutes;
  char type;
  char answer;
    input(hours, minutes);
    hours = convertTo12Hour(hours, type);
    output(hours, minutes, type);

    cout << "Perform another calculation? (y/n): ";
    cin >> answer;

    } while ((answer == 'Y') || (answer == 'y'));

  return 0;
void input(int& hours, int& minutes) {
  cout << "Enter the hours for the 24 hour time: ";
  cin >> hours;
  cout << "Enter the minutes for the 24 hour time: ";
  cin >> minutes;
// Displays a time in 12 hour notation
void output(int hours, int minutes, char type) {
  cout << "The time converted to 12 hour format is: " << hours << ":";  
  // special handling for leading 0s on the minutes
  cout << minutes;
  if (type == 'A')
    cout << " A.M." << endl;
    cout << " P.M." << endl;
int convertTo12Hour(int hours, char& type);{


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 4 Answers and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros