![]() |
|
#1
|
|||
|
|||
Creating a time converterOk, I want to build a time converter, which takes numbers from 24-hour time and converts it to time with AM/PM. I want the output to look something like this (through using c++):
Enter 24 hour time in the format HH:MM 00:20 Time in 12 hour format: 12:20 AM Would you like to do another conversion? (`Y' or `y' for yes; `N' or `n' for No): Y I'm trying to do this by using the call by reference (byref) function to place in A or P (am or pm) in the proper instance. Could someone point me in the right direction, maybe through some wording, not with code. |
|
#2
|
||||
|
||||
Re: Creating a time converterSubract 12 from the hour if > 12...
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
|||
|
|||
Re: Creating a time converterQuote:
ok, i've got that part thought out.. I don't know how I would call a byref to use AM and PM. :S thanks. |
|
#4
|
|||
|
|||
Re: Creating a time converterThis is my base for the program. I have the output subtracting 12 when time is 12 or greater, but I do not know how to create a call for a byref function in each the Am or Pm. My final problem is that I do not know how to make either my hours or minutes be two zeros, such as 00:50 or 12:00
CPP / C++ / C Code:
also i have 2 problems, and I do not know how to debug . D:\Program Files\Microsoft Visual Studio\MyProjects\ergaergaergaerg\wregfaerge.cpp(3 D:\Program Files\Microsoft Visual Studio\MyProjects\ergaergaergaerg\wregfaerge.cpp(3 9) : warning C4508: 'main' : function should return a value; 'void' return type assumed |
|
#5
|
||||
|
||||
Re: Creating a time converterHi tyler,
The problem is that, you misplaced the braces. The closing brace: "}" above the return statement should come before the while loop. like this: CPP / C++ / C Code:
There is no need to use function call by reference for calculating AM or PM. Assume that the time is AM and store it in a string. like: CPP / C++ / C Code:
If the hours is greater than 12, then change the string to "PM". Then print this string after you print the date. Regarding putting extra zero, you can use a simple method of printing a "0" if the minutes is less than 10. like: CPP / C++ / C Code:
There is one more error in the program. Do you know what it is? Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#6
|
|||
|
|||
Re: Creating a time converterfirstly, I don't quite understand your logic for if (minutes < 10) care to indulge me? Lastly, my stupidity on the bracket thing was funny, but your solution fixed all the noticable problems. What's my error? ALSO for some reason my prof has skipped string variables, but i can deal with them just as a char, int, etc. right?
EDIT: nvm... now i get the < 10 thing, so i would do something like cout "0" << minutes? |
|
#7
|
||||
|
||||
Re: Creating a time converterQuote:
I used that logic because you simply cant print "00" using an integer. So, if minutes is less than 10, or hour is less than 10, we should precede that by 0 isnt it? For example, if the time is 5 : 5, we should print it as:<br>05:05 isnt it? So. thats a simple way of preceding it by zeroes. If you havent come up to strings, then use CPP / C++ / C Code:
Regarding the error: Run the program once and check. It dont asks for the input whether to continue or not... How does this error happen? Because you use the cin statement. A simple way to solve this problem is to use a getchar() after your cin statement. edit: Yes. print : cout << "0" << minutes; Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#8
|
|||
|
|||
Re: Creating a time converterThis is how I interpreted the "0" part... we'll see if it's a good way. I hope i didn't mess up the char [] variable. :
I realize that "int minutes = 00;" is pointless, but my cout 0 isn't working when i input 12 00 CPP / C++ / C Code:
|
|
#9
|
||||
|
||||
Re: Creating a time converterGood work.
But we need to have some changes... Here is what the output i got: Code:
Do you see what the error is? Use else if statements. and: Code:
and still not working in the input section. Try solving this one.. What does this statement mean: CPP / C++ / C Code:
Instead, you should vary the string AM or PM. edit: Also, it should be hours = hours - 12; You can simplify the whole process by using only one character: CPP / C++ / C Code:
![]() Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#10
|
|||
|
|||
Re: Creating a time converterWaltP had the (hours - 12) idea, it fits my purpose well.. but you're right because I was having a lot of trouble incorporating it with if statements.
EDIT: also if i put an 'n' or 'N' in to leave the program I get an endless loop... well.. the second time. EDIT of an EDIT: i'm looking for that input error the input i'm taking in isn't taken care of very well. if someone puts in 11 00 then it won't put the AM after it, and things like that, i'm going to have to make separate if statements but that isn't a huge issue. for the input error, I was thinking maybe if nothing is entered, assume it as 00 ? |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Knights Tour - Reloaded . | kobi_hikri | C Programming Language | 12 | 03-Oct-2005 13:15 |
| Simulation Problem | wu_weidong | C++ Forum | 7 | 12-Mar-2005 23:56 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 16:13 |
| time Problem | zuzupus | MySQL / PHP Forum | 9 | 24-Jul-2003 08:02 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The