Link to home
Start Free TrialLog in
Avatar of parvenu51
parvenu51

asked on

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

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:

[CODE]
#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;
 
  do
   {
    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.width(2);
  cout.fill('0');
  cout << minutes;
  //
  if (type == 'A')
    cout << " A.M." << endl;
  else
    cout << " P.M." << endl;
 }
 
int convertTo12Hour(int hours, char& type);{
      

SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> you can try on following lines:

Why do you again post the full code, lucky_james ?

@parvenu51 : please ignore that code, and try to do this yourself ...
@james,
According to EE rules, you cannot provide a full answer to a question that appears to be a homework, even it is not elegant in the case the user is trying to self-teach.
In those cases it is better to provide some clues or a program template.
@infinity
            I respect ur point but try to understand that this code was merely typed by me to explain my algorithm. if it works then, its by chance only. Otherwise, no body would first explain the algorithm and then write the code as well. I would have pasted the code directly.
<<<<< again posted......
              the previous posted code was proved to be the non working version whose non-working state was verified by its author himself.

@jaime
          I got ur point, but as i have explained above i am not the person who has either learned c++ through shortcuts or want others to learn that way. Hardships are what matter and i know that.

If not satisfied, see my other posts......try to observe what was the point behind the sample which i may or may not give in every posts but for sur, if i give, there has to be some point to explain.

Best Regards,
..
>> I got ur point, but as i have explained above i am not the person who has either learned c++ through shortcuts or want others to learn that way. Hardships are what matter and i know that.

Besides your personal opinion, there are specific rules at EE about homework answering. It is not a democracy ;)
>>>>>Besides your personal opinion, there are specific rules at EE about homework answering. It is not a democracy ;)
well, EE rules ask us not to post the exact code........and i feel i have already said a lot about it being the the exact one rather than an explanation. :)

Regards,
..
After 11:59 AM time will become 12:00PM or (00:00PM)
all above logics says till 12:59  is AM
so we cannot check like (hours >12), you need to pass minutes as well
like this
void convertTo12Hour(int& hours, int min, char& type)
{
   type = (hours*100 + min  >= 1200) ? 'B' : 'A';
   if (hours >12)   // changes hour only if hour reaches 13 hr
       hours-=12;
  /*  if you need to print 12:00 as 00:00 pm then you can simply use below stmt instead of above if condition
     hours %=12;
*/
}
Sorry for posting code itself.
lucky_james : that's entirely besides the point. It doesn't matter with what intention you posted the code ... what matters is that you DID post the code.

You can read up on the rules here :

        https://www.experts-exchange.com/help.jsp#hi105

and here :

        https://www.experts-exchange.com/help.jsp#hi130



>> so we cannot check like (hours >12)

You can always check for >= 12 instead ;)
well, thanks for reminding them infinity!! I have already gone thru them when i joined them. I have already defined the line. i wont like beating around the bush.

I respect the arena and its rules else i would not have joined it.


thanks.
..