#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    4
    Rep Power
    0

    Unhappy 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

    #include<stdio.h>
    int main()
    {
    int hour;char am_or_pm;
    printf("diplay time in 12 hour form:\n");
    printf("--------------------------------\n\n");
    printf("Enter a 24-hour time:");
    scanf("%d",&hour);
    if (hour<=11)
    am_or_pm='am';
    else {
    hour+=12;
    am_or_pm='pm';
    }
    if (hour==0)
    hour=12;
    printf("%d");
    return 0;
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    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.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    4
    Rep Power
    0
    thanks, but you know that was a type error
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    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;?
    Code:
    if (hour<=11) 
        am_or_pm='am'; 
    else 
    { 
        hour+=12; 
        am_or_pm='pm'; 
    }

IMN logo majestic logo threadwatch logo seochat tools logo