Thread: Incrementing minutes for 12hour time?

  1. #16
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by Warcom View Post
    for some reason when the hour switches to 1 it actually prints 13:00 then it goes to 1:01?
    Hello??!! That's the entire point of my post!!!

  2. #17
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    My apologies algorism I did not pick up on what you meant by that.
    [ QUOTE=algorism;1264924]Hello??!! That's the entire point of my post!!![/QUOTE]

  3. #18
    Registered User
    Join Date
    Jan 2017
    Posts
    12

    Thank You algorism!

    That worked perfect for the hr change. Do you have any hints for me as to why the P will switch to A but the A will not switch to P?
    Quote Originally Posted by algorism View Post
    Also note that you're wrapping hr in the wrong place (before you increment it!). It should be something like:
    Code:
            if (++min > 59) {
                min = 0;
                if (++hr > 12)
                    hr = 1;
            }

  4. #19
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by Warcom View Post
    That worked perfect for the hr change. Do you have any hints for me as to why the P will switch to A but the A will not switch to P?
    Again, it's a matter of the relative position of the code. Instead of testing if hr is 1 (which it will be over and over again from 1:00 to 1:59), put the am/pm code in where hr is set to 1.
    Code:
    if (++min > 59) {
        min = 0;
        if (++hr > 12) {
            hr = 1;
            // put the am/pm code in here (but don't bother checking for hr == 1 since we already know that)
        }
    }
    EDIT: Actually, now that I think about it that's not quite right. It's more like:
    Code:
    if (++min > 59) {
        min = 0;
        if (++hr == 12) {
            // put the am/pm code in here
        }
        else if (hr > 12)
            hr = 1;
    }
    Last edited by algorism; 01-26-2017 at 04:00 PM.

  5. #20
    Registered User
    Join Date
    Jan 2017
    Posts
    12

    Well things are really mess up now

    Quote Originally Posted by algorism View Post
    Again, it's a matter of the relative position of the code. Instead of testing if hr is 1 (which it will be over and over again from 1:00 to 1:59), put the am/pm code in where hr is set to 1.
    Code:
    if (++min > 59) {
        min = 0;
        if (++hr > 12) {
            hr = 1;
            // put the am/pm code in here (but don't bother checking for hr == 1 since we already know that)
        }
    }
    EDIT: Actually, now that I think about it that's not quite right. It's more like:
    Code:
    if (++min > 59) {
        min = 0;
        if (++hr == 12) {
            // put the am/pm code in here
        }
        else if (hr > 12)
            hr = 1;
    }
    I added what you suggested previously and my only remaining problem was the AM not switching to PM

    But now not even the minutes are incrementing properly

    Any idea where I went wrong?
    Code:
        for(i=0;i<addtime;i++)
                            {
                            if (++min > 59)
                             {
                            min = 0;
                             }
                            if (++hr == 12 && ampm=='P')
                                    {
                                    ampm='A';
                                    }
                                    if (++hr == 12 && ampm=='A')
                                            {
                                    ampm='P';
                                    }
                                    else if(hr>12)
                                    {
                                    hr=1;
                                    }
    
    
    
    
             printf("%d:%.02d %cM\n",hr,min,ampm);

  6. #21
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    That is a bit of a mess. It's important to indent things properly so you can see what's going on. I believe the following is correct. Note that it's a matter of putting things in the right place so they are controlled by the right conditions.
    Code:
        for (i = 0; i < addtime; i++)
        {
            if (++min > 59)
            {
                min = 0;
                if (++hr == 12)
                {
                    if (ampm == 'A')
                        ampm = 'P';
                    else
                        ampm = 'A';
                }
                else if (hr > 12)
                    hr = 1;
            }
    
            printf("%d:%.02d %cM\n", hr, min, ampm);
        }

  7. #22
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    Much better. Thanks for your help!

  8. #23
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    One last problem....

    How do I validate that input is numeric? If I enter a for minutes the program will just start a never ending loop.

  9. #24
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Check the return value of scanf() - it returns the number of successful conversions. If invalid data is entered (e.g. zero conversions were performed), the offending input remains on the input buffer and needs to be cleared. See here for suggestions on how to clear the input buffer in this situation: FAQ > Flush the input buffer - Cprogramming.com

    A better solution would be to read an entire line using fgets(), and extract the data from the string using sscanf(). This will eliminate the need to clear the input buffer if bad input is entered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Time converting problem. (24-hour military format to minutes)
    By heavymetalbagel in forum C Programming
    Replies: 5
    Last Post: 08-08-2011, 11:04 AM
  2. Incrementing a number each time a function is called.
    By Coder87C in forum C++ Programming
    Replies: 17
    Last Post: 01-31-2005, 07:04 PM
  3. Incrementing time
    By Farls in forum C Programming
    Replies: 5
    Last Post: 03-19-2002, 05:43 PM
  4. Incrementing time and calendarfrom 1999-2000
    By Karma in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2001, 02:48 PM
  5. Converting MInutes to Hours and MInutes
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-30-2001, 08:07 PM

Tags for this Thread