We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,289 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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:

#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 ?

10
Contributors
27
Replies
3 Years
Discussion Span
8 Months Ago
Last Updated
30
Views
rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

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

rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

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!

Ancient Dragon
Achieved Level 70
Team Colleague
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

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?

rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

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;

}
BevoX
Junior Poster in Training
57 posts since Jan 2009
Reputation Points: 77
Solved Threads: 13
Skill Endorsements: 0

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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

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

rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

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

rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

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';
}
Ancient Dragon
Achieved Level 70
Team Colleague
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

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

rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0
void Convert( int& hours )
   
      {
   
      hours = hours % 12;
   
      }
	  
	  if( hours < 12)
{
    pm = 'A';
}

	  else;
{
    hours -= 12;
    pm = 'P';
}
rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

im getting errrors associated with that column

rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

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

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

Ancient Dragon
Achieved Level 70
Team Colleague
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

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

rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

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;
}
JameB
Junior Poster
166 posts since Mar 2009
Reputation Points: 76
Solved Threads: 15
Skill Endorsements: 1

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?

rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0
#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;
}
JameB
Junior Poster
166 posts since Mar 2009
Reputation Points: 76
Solved Threads: 15
Skill Endorsements: 1

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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

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 ?

rock9449
Light Poster
30 posts since May 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1229 seconds using 2.73MB