| | |
How do I write a C++ program that converts from 24-hour notation to 12-hour notation?
![]() |
•
•
Join Date: May 2008
Posts: 30
Reputation:
Solved Threads: 0
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:
but i am stuck on the rest am i on the right track and where do i go from here ?
The code I have so far is:
C++ Syntax (Toggle Plain Text)
#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 ?
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.
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: May 2008
Posts: 30
Reputation:
Solved Threads: 0
Re: How do I write a C++ program that converts from 24-hour notation to 12-hour notation?
0
#3 Apr 7th, 2009
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)
Delete that semicolon!
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: May 2008
Posts: 30
Reputation:
Solved Threads: 0
Re: How do I write a C++ program that converts from 24-hour notation to 12-hour notation?
0
#5 Apr 7th, 2009
Re: How do I write a C++ program that converts from 24-hour notation to 12-hour notation?
-1
#6 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.
C++ Syntax (Toggle Plain Text)
void Convert( int& hours ) { hours = hours % 12; }
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Re: How do I write a C++ program that converts from 24-hour notation to 12-hour notation?
0
#7 Apr 7th, 2009
•
•
•
•
Maybe you can avoid that, I am thinking something like this.
C++ Syntax (Toggle Plain Text)
void Convert( int& hours ) { hours = hours % 12; }
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: May 2008
Posts: 30
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: May 2008
Posts: 30
Reputation:
Solved Threads: 0
Re: How do I write a C++ program that converts from 24-hour notation to 12-hour notation?
0
#9 Apr 7th, 2009
Re: How do I write a C++ program that converts from 24-hour notation to 12-hour notation?
0
#10 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?
C++ Syntax (Toggle Plain Text)
if( hours < 12) { pm = 'A'; } else { hours -= 12; pm = 'P'; }
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
![]() |
Other Threads in the C++ Forum
- Previous Thread: static_cast on member void* pVar but how?
- Next Thread: loading bitmaps
Views: 2078 | Replies: 22
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int lazy link linker list loop looping loops math matrix member memory newbie number object objects opengl operator output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference server sort spoonfeeding string strings struct student studio template templates text time tree undefined url variable vc++ vector video visual win32 window windows winsock wxwidgets






