September 21st, 2003, 07:50 PM
display time in 12 hour form
a program that ask user for a 24-hour time,then displays the time in 12-hour form:
Enter a 24-hour time:21:11
Equivalent 12-hour Time:9:11 pm
becareful not to display 12:00 as 0:00
I coded this program as follows but I dont get an out- put although it compiles without any error
this is the program
int hour;char am_or_pm;
printf("diplay time in 12 hour form:\n");
printf("Enter a 24-hour time:");
September 21st, 2003, 08:52 PM
Please use "code" tags!
I don't understand why your example compiles, you can't assign a value of 'am' to a character!
Your logic looks rather suspect to me, I think you got some bugs.
As for lack of output, according to my reading, you will only get output if you enter zero for the hour.
September 21st, 2003, 09:08 PM
thanks, but you know that was a type error
September 22nd, 2003, 12:10 AM
You gave us false information. You stated that it doesn't give you an output. That is not true. It does indeed give an output; it's just not what you expect. How do you expect us to help you if you feed us false information?
Take a look at that printf statement towards the end. What variable are you telling the printf to print? You aren't, but it's still going to look on the stack for something to print. So basically you're telling it to print whatever garbage it sees.
One of the requirements in programming is to read your code and understand what you are telling the computer to do.
BTW, when you convert 24-hour time to 12-hour time, you need to subtract 12 hours from the times after 12 (2100 -> 9 PM by subtracting 12 from 21). You do not add 12 to the 24-hour time. Why then this code, especially that line, hour+=12;?