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;
 
  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);{

but i am stuck on the rest am i on the right track and where do i go from here ?

First do the math on paper & pencil. Once you get that right then coding it will be simple.

If the hours is greater than 12 just subtract 12 and the A.M./P.M. flag will be 'P'. Otherwise if the hours is less than 12 the flag will be 'A' and you don't have to do anything to the hours.

where do i insert this into my code perhaps i outdid myself and should start all over , am i on the right track?

put it in the convert function that you started (see the last line that you posted) int convertTo12Hour(int hours, char& type);{ Delete that semicolon!

using an if else statement but what function i call i can put it in words but that's where i have a hard problem putting it into code
i know it would be something like
if hours < 12
pm = 12- hours ?
cout>>pm?

First do the math on paper & pencil. Once you get that right then coding it will be simple.

If the hours is greater than 12 just subtract 12 and the A.M./P.M. flag will be 'P'. Otherwise if the hours is less than 12 the flag will be 'A' and you don't have to do anything to the hours.

Maybe you can avoid that, I am thinking something like this.

void Convert( int& hours )
{
	hours = hours % 12;

}

Maybe you can avoid that, I am thinking something like this.

void Convert( int& hours )
{
	hours = hours % 12;

}

The problem is that will not tell whether it's am or pm.

Error 1 error LNK2019: unresolved external symbol "int __cdecl convertTo12Hour(int,char &)" (?convertTo12Hour@@YAHHAAD@Z) referenced in function _main lab4.obj lab4
Error 2 fatal error LNK1120: 1 unresolved externals C:\Documents and Settings\oml5000\My Documents\Visual Studio 2008\Projects\lab4\Debug\lab4.exe lab4


it almost works but im still getting two compiler errors something with regards to my conversion

yeah i see the problem that's why i think its giving me a compiler error so after the convert statement i think i need an if else statement to tell it to differentiate between AM or Pm

using an if else statement but what function i call i can put it in words but that's where i have a hard problem putting it into code
i know it would be something like
if hours < 12
pm = 12- hours ?
cout>>pm?

if( hours < 12)
{
    pm = 'A';
}
else
{
    hours -= 12;
    pm = 'P';
}

yes almost but i think the if else syntax is wrong?

void Convert( int& hours )
   
      {
   
      hours = hours % 12;
   
      }
	  
	  if( hours < 12)
{
    pm = 'A';
}

	  else;
{
    hours -= 12;
    pm = 'P';
}

im getting errrors associated with that column

yes almost but i think the if else syntax is wrong?

you mean what I posted before? why do you think it's wrong?

because i am getting complier errors when added onto my new 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;
 }
 
   
      void Convert( int& hours )
   
      {
   
      hours = hours % 12;
   
      }
	  if( hours < 12)
{
    pm = 'A';
}
else
{
    hours -= 12;
    pm = 'P';
}

errors
Error 1 error C2059: syntax error : 'if'
Error 2 error C2143: syntax error : missing ';' before '{' 65 lab4
Error 3 error C2447: '{' : missing function header (old-style formal list?) 65 lab4
Error 4 error C2059: syntax error : 'else' c
Error 5 error C2143: syntax error : missing ';' before '{' lab4
Error 6 error C2447: '{' : missing function header (old-style formal list?)

they all centralize around the convert and if else statement

Why do you need to use this many functions do write such a simple program??

Take a look at what I did,

#include<iostream>
#include<string>
using namespace std;

int main()
{
	int hours, minutes;
	string ampm; // stores AM or PM depending on the value of 'hours'

	cout << "Enter the hour followed by a space followed by the minutes." << endl;
	cin >> hours >> minutes;

	if(hours > 12) // If it is bigger than 12 then you take out 12 from it.
	{
		hours -= 12;
		ampm = " PM";
	}
	else // If not, then do not do anything to it and just make 'ampm' "AM"
	{
		ampm = " AM";
	}
	
	cout << hours << ':' << minutes << ampm << endl; // Output the values

	return 0;
}

also it asks me to included a lopp that lets users inter in new value until they wish to end the program
how would that be done with a while loop that would let the user enter in a value or a letter to quit the program?

#include<iostream>
#include<string>
using namespace std;

int main()
{
	int hours, minutes;
	string ampm, choice; // stores AM or PM depending on the value of 'hours'


	while(1)
	{

		cout << "Enter hours and then space then the minutes" << endl;
		cin >> hours >> minutes;

		if(hours > 12) // If it is bigger than 12 then you take out 12 from it.
		{
			hours -= 12;
			ampm = " PM";
		}
		else // If not, then do not do anything to it and just make 'ampm' "AM"
		{
			ampm = " AM";
		}
		
		cout << hours << ':' << minutes << ampm << endl; // Output the values

		cout << "Would you like to re-run the program? (Yes or No)" << endl;
		cin >> choice;

		if(! (choice == "yes" || choice == "YES" || choice == "Yes")) // If the choice is NOT yes then it will break out of the infinite loop...
		{
			break;
		}

	}

	return 0;
}

because i am getting complier errors when added onto my new 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;
 }
 
   
      void Convert( int& hours )
   
      {
   
      hours = hours % 12;
   
      }
	  if( hours < 12)
{
    pm = 'A';
}
else
{
    hours -= 12;
    pm = 'P';
}

errors
Error 1 error C2059: syntax error : 'if'
Error 2 error C2143: syntax error : missing ';' before '{' 65 lab4
Error 3 error C2447: '{' : missing function header (old-style formal list?) 65 lab4
Error 4 error C2059: syntax error : 'else' c
Error 5 error C2143: syntax error : missing ';' before '{' lab4
Error 6 error C2447: '{' : missing function header (old-style formal list?)

they all centralize around the convert and if else statement

You have all those errors because you failed to put that if statement inside any function. Check your { and } and you will see your mistake.

thank you so much sir , how do you get so good at this is it just from reading or is it just constant coding or ?

thank you so much sir , how do you get so good at this is it just from reading or is it just constant coding or ?

From many years of coding experience, reading and listening to others. The more you practice (coding) the better you will get. You don't learn how to drive a car overnight either.

Hi I am learning programming at Uni so I am new to this but I wrote this code that seems to work pretty well

#include <iostream>
using namespace std;

int main()
{

	int hour, minute;
	int htime;
	cout << "Enter a 24 hour time in hours then in minutes: ";
	cin >> hour >> minute;

	if  ((hour <= 23) && (hour >= 12)) {
		htime = (hour - 12);
		cout << htime << ":" << minute << " PM";
	}

	else if ((hour <= 11) && (hour >= 00))
		cout << hour << ":" << minute << " AM";

	return 0;

}

Some of the solutions posted here are not quite correct. Zero in 24 hour time is 12:00 AM. Try this:

void convertTo12Hour(int hour24, int& hour12, char& ampm)
{
    hour12 = hour24 % 12;
    if (hour12 == 0)
        hour12 = 12;
    ampm   = (hour24 < 12) ? 'a' : 'p';
}
commented: Nice solution :) +36

plz tel 12 to 24 hors conversion?

plz tel 12 to 24 hors conversion?

This is 2nd grade math problem. If the hour is 12:00 noon to midnight just add 12 hours.

Hey this is my code:Hope it helps ! !

=========================================================================================

#include <iostream>
using namespace std;
int  main()
{
     // declare variables
     int time = 0;
     int hour = 0;
     int min = 0;
     int sec = 0;

     // obtain data from user
     cout << "Enter a time in seconds: ";
     cin >> time;

     // using the time from ^ above, convert
     // secs to HH:MM:SS format using division
     // and modulus
     hour=time/3600;
     time=time%3600;
     min=time/60;
     time=time%60;
     sec=time;

     // display data to user
     cout<<"\nThe time in HH:MM:SS format is: "<<hour<<" hours, "
          <<min<<" minutes, and "<<sec<<" seconds!\n";

     return 0;
}
commented: Was that really necessary to revive a 3 year old thread? Use the code snippet section of this forums. -1

// using the time from ^ above, convert
// secs to HH:MM:SS format using division
// and modulus

Or, just simply call localtime() or gmtime(), which will put it in a structure for you.

Or, just simply call localtime() or gmtime(), which will put it in a structure for you.

But, but that would defeat the purpose of reinventing (poorly) the behavior that someone worked very hard to implement in the standard library! ;p

//Name Syed zain Ali
// Section A
//***********************************************************************************************
// 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 function:
// 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,
// and function(s) to display the results. (For 12-hour time notation,
// your program must display AM or PM.)
//*********************************************************************************************
#include <iostream>
using namespace std;

//
// function declarations
//

void output();
void time(int &choice);
void input(int& hours, int& minutes, char&type,int &choice,int& hours1);
void output(int& hours, int minutes, char type,int &choice, int& hours1);
void output(int &choice, int& hours1);
int main()
{
int hours,hours1;
int minutes;
char type;
char answer;
int choice;





do {
 output();
  time(choice);
  input( hours,  minutes, type,choice, hours1);
   output( hours,  minutes,  type, choice,  hours1);
    output(choice, hours1);

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

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



return 0;
}
//******************************
//Function defination
//*****************************
//*******ouuput function*************
void output()
{
    cout<<"\t\t\t**************************************"<<endl;
    cout<<"\t\t\t A program that converts time notation"<<endl;
    cout<<"\t\t\t**************************************"<<endl;
}
//*******choice function*************
void time(int &choice)
{
cout<<"Enter the Notation in which You want to enter the time (24 or 12):";
cin>>choice;
}
//*******input function*************
void input(int& hours, int& minutes, char&type,int &choice,int& hours1)
{
    cout<<"\t\t\t**************************************"<<endl;
cout<<"\t\t\t     You choice" <<choice<<"hour time format:"<<endl;
cout<<"\t\t\t**************************************"<<endl;

if(choice==24)
{


cout << "Enter the hours for the 24 hour time: ";
cin >> hours1;


// prompt the user for the number of minutes

}
else
    {
        cout << "Enter the hours for the 12 hour time: ";
cin >> hours;

    cout << "\nEnter the minutes for the 12 hour time: ";
cin >> minutes;
cout<<"Enter the type of time (A.m or P.m):";
cin>>type;
}
}
void output(int& hours, int minutes, char type,int &choice, int& hours1)
{

//
// //*******ouuput function*************
//
if(choice==12)
{
             int d=0;


    if (hours>12&&hours<24)
    {
        d=hours-12;
    }
    else if (hours<12)
    {
        d=hours+12;
    }
  cout<<d;


cout << "The time converted to 12 hour format is: " <<d << ":";

//
// special handling for leading 0s on the minutes
//

cout.width(2);
cout.fill('0');
cout << minutes;

//
// Output A.M. or P.M. based on type.
//

if (type == 'A'||type=='a')
cout << " A.M." << endl;
else
cout << " P.M." << endl;
}


}
void output(int &choice, int& hours1)
{
    int res=0,res1=0,time=0,res2=0;
    if(choice==24)
        {

        if(hours1>=1200&&hours1<=2400)
           {


    res=hours1/100;
    time=res-12;
    res1=hours1%100;
        if(res1==0)
        {
                cout << "The time converted to 12 hour format is: " <<time << ":"<<res1<<"0"<<"(A.m/P.m)";
        }
        else
            {
            cout << "The time converted to 12 hour format is: " <<time << ":"<<res1<<"(A.m/P.m)";
            }
        }
        if(hours1>0&&hours1<1200)
            {

            time=1200+hours1;
            res=time/100;
            res1=time%100;
            if(res1==0)
            {
                cout << "The time converted to 12 hour format is: " <<res << ":"<<res1<<"0"<<"(A.m/P.m)";

            }
            else
                {
                    cout << "The time converted to 12 hour format is: " <<res << ":"<<res1<<"(A.m/P.m)";

                }
            }
        }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.