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

Reply

Join Date: May 2008
Posts: 30
Reputation: rock9449 is an unknown quantity at this point 
Solved Threads: 0
rock9449 rock9449 is offline Offline
Light Poster

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

 
0
  #1
Apr 7th, 2009
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:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. void input(int& hours, int& minutes);
  6. void output(int hours, int minutes, char type);
  7. int convertTo12Hour(int hours, char& type);
  8.  
  9. int main() {
  10. int hours;
  11. int minutes;
  12. char type;
  13. char answer;
  14.  
  15. do
  16. {
  17. input(hours, minutes);
  18. hours = convertTo12Hour(hours, type);
  19. output(hours, minutes, type);
  20.  
  21. cout << "Perform another calculation? (y/n): ";
  22. cin >> answer;
  23.  
  24. } while ((answer == 'Y') || (answer == 'y'));
  25.  
  26. return 0;
  27. }
  28.  
  29. void input(int& hours, int& minutes) {
  30. cout << "Enter the hours for the 24 hour time: ";
  31. cin >> hours;
  32. cout << "Enter the minutes for the 24 hour time: ";
  33. cin >> minutes;
  34. }
  35. //
  36. // Displays a time in 12 hour notation
  37. //
  38. void output(int hours, int minutes, char type) {
  39. cout << "The time converted to 12 hour format is: " << hours << ":";
  40. //
  41. // special handling for leading 0s on the minutes
  42. //
  43. cout.width(2);
  44. cout.fill('0');
  45. cout << minutes;
  46. //
  47. if (type == 'A')
  48. cout << " A.M." << endl;
  49. else
  50. cout << " P.M." << endl;
  51. }
  52.  
  53. 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 ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,605
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Apr 7th, 2009
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.
Last edited by Ancient Dragon; Apr 7th, 2009 at 8:45 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 30
Reputation: rock9449 is an unknown quantity at this point 
Solved Threads: 0
rock9449 rock9449 is offline Offline
Light Poster

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

 
0
  #3
Apr 7th, 2009
where do i insert this into my code perhaps i outdid myself and should start all over , am i on the right track?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,605
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #4
Apr 7th, 2009
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!
Last edited by Ancient Dragon; Apr 7th, 2009 at 8:49 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 30
Reputation: rock9449 is an unknown quantity at this point 
Solved Threads: 0
rock9449 rock9449 is offline Offline
Light Poster

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

 
0
  #5
Apr 7th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

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

 
-1
  #6
Apr 7th, 2009
Originally Posted by Ancient Dragon View Post
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.
  1. void Convert( int& hours )
  2. {
  3. hours = hours % 12;
  4.  
  5. }
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,605
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #7
Apr 7th, 2009
Originally Posted by BevoX View Post
Maybe you can avoid that, I am thinking something like this.
  1. void Convert( int& hours )
  2. {
  3. hours = hours % 12;
  4.  
  5. }
The problem is that will not tell whether it's am or pm.
Last edited by Ancient Dragon; Apr 7th, 2009 at 9:02 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 30
Reputation: rock9449 is an unknown quantity at this point 
Solved Threads: 0
rock9449 rock9449 is offline Offline
Light Poster

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

 
0
  #8
Apr 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 30
Reputation: rock9449 is an unknown quantity at this point 
Solved Threads: 0
rock9449 rock9449 is offline Offline
Light Poster

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

 
0
  #9
Apr 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,605
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #10
Apr 7th, 2009
Originally Posted by rock9449 View Post
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?
  1. if( hours < 12)
  2. {
  3. pm = 'A';
  4. }
  5. else
  6. {
  7. hours -= 12;
  8. pm = 'P';
  9. }
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 2078 | Replies: 22
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC