Thread: Incrementing minutes for 12hour time?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    12

    Incrementing minutes for 12hour time?

    Hello I am trying to take input from user for a given 12 hour clock time, then ask how many minutes to add to it, then print increments of each minute until the sum of those minutes is reached. I am having trouble with incrementing to the neccessary hour. Any ideas?
    Code:
    #include <stdio.h>
    main()
    {
            int addtime,stophr,stopmin,hr=12,min=0;
    
    
    
    
            printf("How many minutes do you want to add?");
            scanf("%d",&addtime);
            stophr=(min + addtime/60) + hr;
            stopmin=(min + addtime)%60;
            if(stophr>12)
            {
            stophr=stophr-12;
            }
            if(stopmin>59)
            {
            stopmin=stopmin-60;
            }
            printf("%d:%.02d\n",hr,min);
            while(hr!=stophr && min!=stopmin)
            {
                    min++;
            if(hr>12)
            {
            hr=hr-12;
            }
            if(min>59)
            {
            hr=hr+1;
            min=0;
            }
    
    
            printf("%d:%.02d\n",hr,min);
            }
    
    
            printf("%d:%.02d\n",stophr,stopmin);
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First, you should make sure your code is neatly formatted and indented (i.e. all code within brackets should be indented one level):

    Code:
    #include <stdio.h>
    
    int main(void)  /* corrected main() header */
    {
        int addtime,stophr,stopmin,hr=12,min=0;
    
        printf("How many minutes do you want to add?");
        scanf("%d",&addtime);
    
        stophr=(min + addtime/60) + hr;
        stopmin=(min + addtime)%60;
    
        if(stophr>12)
        {
            stophr=stophr-12;
        }
        if(stopmin>59)
        {
            stopmin=stopmin-60;
        }
    
        printf("%d:%.02d\n",hr,min);
    
        while(hr!=stophr && min!=stopmin)
        {
            min++;
    
            if(hr>12)
            {
                hr=hr-12;
            }
            if(min>59)
            {
                hr=hr+1;
                min=0;
            }
    
             printf("%d:%.02d\n",hr,min);
        }
    
        printf("%d:%.02d\n",stophr,stopmin);
    }
    The problem lies in your loop condition:

    Code:
    while(hr!=stophr && min!=stopmin)
    For example, if we enter 70, then "stopmin" is set to 10. Once "min" reaches 10 the first time, the loop condition fails.

    Instead of looking for the "stop hour" and "stop minute", you might consider controlling your loop with a simple counter that counts up to the maximum number of minutes.

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    How would one code that?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Warcom View Post
    I get that it would be better to not have stophr and stopmin. But I guess I don't know how to increment the time to the required stop time.
    Say 300 minutes are the addtime what Boolean would I use to loop the increment ?
    You don't even need the "stop time". The body of the loop should just continue incrementing hours/minutes, while the loop condition should be controlled by a separate (counter) variable.

    If you've learned about for() loops, this would be a perfect application to use one.

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    Thank you Matticus,
    I was thinking about a for loop but I was wondering how to set it to only increment the number of added minutes.
    say the number is 300 can I go
    for (min<addtime; min++) ?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Warcom View Post
    Thank you Matticus,
    I was thinking about a for loop but I was wondering how to set it to only increment the number of added minutes.
    say the number is 300 can I go
    for (min<addtime; min++) ?
    Yes, you are on the right track, since "addtime" contains the total number of minutes. Be sure to use a separate variable for the counter, as "min" is used for other purposes.

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    Quote Originally Posted by Warcom View Post
    Hello I am trying to take input from user for a given 12 hour clock time, then ask how many minutes to add to it, then print increments of each minute until the sum of those minutes is reached. I am having trouble with incrementing to the neccessary hour. Any ideas?
    Code:
    #include <stdio.h>
    main()
    {
            int addtime,stophr,stopmin,hr=12,min=0;
    
    
    
    
            printf("How many minutes do you want to add?");
            scanf("%d",&addtime);
            stophr=(min + addtime/60) + hr;
            stopmin=(min + addtime)%60;
            if(stophr>12)
            {
            stophr=stophr-12;
            }
            if(stopmin>59)
            {
            stopmin=stopmin-60;
            }
            printf("%d:%.02d\n",hr,min);
            while(hr!=stophr && min!=stopmin)
            {
                    min++;
            if(hr>12)
            {
            hr=hr-12;
            }
            if(min>59)
            {
            hr=hr+1;
            min=0;
            }
    
    
            printf("%d:%.02d\n",hr,min);
            }
    
    
            printf("%d:%.02d\n",stophr,stopmin);
    }
    The part that is giving me the trouble is how to get the increments of minutes to stop at the added time. printf("%d:%.02d\n",hr,min);

    while(hr!=stophr && min!=stopmin)

    the output will print the right start time but won't increment all the way to the stop time.

  8. #8
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    I get that it would be better to not have stophr and stopmin. But I guess I don't know how to increment the time to the required stop time.
    Say 300 minutes are the addtime what Boolean would I use to loop the increment ?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You need another variable to act as a counter.
    You need a loop condition that checks this counter value against the total number of minutes that need to be counted.

    Expressing logic via code is a learned skill, so spend a little time mulling it over, then attempt to implement it yourself. If you get stuck, post your updated code along with specific questions, and we can go from there.

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    To determine the correct while condition think about when you want it to stop. You want it to stop when hr==stophr && min==stopmin, therefore you want it to continue while (!(hr==stophr && min==stopmin)), or (by deMorgan's laws), while (hr != stophr || min != stopmin).

    However, this won't work for addtime greater than or equal to 12 * 60. So it would be better to just use a counter as Matticus said.

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    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;
            }
    Last edited by algorism; 01-26-2017 at 12:03 PM.

  12. #12
    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;
            }

  13. #13
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    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.

  14. #14
    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);

  15. #15
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Or simply:
    Code:
    while (addtime-- > 0) {
        ...
    }

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