943,920 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4738
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 7th, 2009
0

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

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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 ?
Reputation Points: 46
Solved Threads: 0
Light Poster
rock9449 is offline Offline
30 posts
since May 2008
Apr 7th, 2009
0

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

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 9:45 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 7th, 2009
0

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

where do i insert this into my code perhaps i outdid myself and should start all over , am i on the right track?
Reputation Points: 46
Solved Threads: 0
Light Poster
rock9449 is offline Offline
30 posts
since May 2008
Apr 7th, 2009
0

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

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 9:49 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 7th, 2009
0

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

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?
Reputation Points: 46
Solved Threads: 0
Light Poster
rock9449 is offline Offline
30 posts
since May 2008
Apr 7th, 2009
-1

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

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.
C++ Syntax (Toggle Plain Text)
  1. void Convert( int& hours )
  2. {
  3. hours = hours % 12;
  4.  
  5. }
Reputation Points: 77
Solved Threads: 12
Junior Poster in Training
BevoX is offline Offline
57 posts
since Jan 2009
Apr 7th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by BevoX ...
Maybe you can avoid that, I am thinking something like this.
C++ Syntax (Toggle Plain Text)
  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 10:02 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 7th, 2009
0

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

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
Reputation Points: 46
Solved Threads: 0
Light Poster
rock9449 is offline Offline
30 posts
since May 2008
Apr 7th, 2009
0

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

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
Reputation Points: 46
Solved Threads: 0
Light Poster
rock9449 is offline Offline
30 posts
since May 2008
Apr 7th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by rock9449 ...
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?
C++ Syntax (Toggle Plain Text)
  1. if( hours < 12)
  2. {
  3. pm = 'A';
  4. }
  5. else
  6. {
  7. hours -= 12;
  8. pm = 'P';
  9. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Equal Objects
Next Thread in C++ Forum Timeline: 2D OpenGL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC