Thread: Incrementing minutes for 12hour time?

  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
    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.

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

  5. #5
    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 ?

  6. #6
    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.

  7. #7
    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.

  8. #8
    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.

  9. #9
    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++) ?

  10. #10
    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.

  11. #11
    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.

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

  13. #13
    Registered User
    Join Date
    Jan 2017
    Posts
    12

    Got it Thanks Matticus

    Code:
    #include <stdio.h>
    
    
    int main(void)  /*  */
    {
        int addtime,hr=12,min=0,i=0;
    
    
        printf("How many minutes do you want to add?");
        scanf("%d",&addtime);
    
    
    
    
        printf("%d:%.02d\n",hr,min);
    
    
             for(i=0;i<addtime;i++)
            {
            min++;
    
    
            if(hr>12)
            {
                hr=hr-12;
            }
            if(min>59)
            {
                hr=hr+1;
                min=0;
            }
    
    
             printf("%d:%.02d\n",hr,min);
        }
    
    
        }
      Now I need to figure out how to switch from Am to Pm:eek: I may be back with more questions soon.  Thanks for the hints.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome. I suggest you run the program with an input of 70, look at the output, then read the advice given by algorism from post #10.

    Also, be sure to keep your code properly indented, as I've suggested.

  15. #15
    Registered User
    Join Date
    Jan 2017
    Posts
    12

    70 works fine except....

    Quote Originally Posted by Matticus View Post
    You're welcome. I suggest you run the program with an input of 70, look at the output, then read the advice given by algorism from post #10.

    Also, be sure to keep your code properly indented, as I've suggested.
    Code:
    #include <stdio.h>
    
    
    int main(void)  /*  */
    {
        int addtime,hr,min,i=0;
        char ampm;
            printf("Please enter the hour:");
            scanf("%d",&hr);
                    while(hr<0||hr>12)
                    {
                    printf("Please enter a number between (1-12)only:\n");
                    scanf("%d",&hr);
                    }
            printf("Please enter the minutes:\n");
            scanf("%d",&min);
                    while(min<0||min>59)
                    {
                    printf("Please enter a number between(0-59):\n");
                    scanf("%d",&min);
                    }
            printf("Please enter 'A' for AM or 'P' for PM:\n");
            scanf("%c",&ampm);
                    while(!(ampm=='A'||ampm=='P'))
                    {
                    printf("'A' or 'P' only please:\n");
                    scanf("%c",&ampm);
                    }
    
    
            printf("How many minutes do you want to add?");
            scanf("%d",&addtime);
                    while(addtime<1||addtime>800)
                    {
                    printf("Please enter a number between 1-800 only:\n");
       scanf("%d",&addtime);
                    }
    
    
             printf("%d:%.02d %cM\n",hr,min,ampm);
    
    
                     for(i=0;i<addtime;i++)
                            {
                          min++;
    
    
                  if(hr>12)
                    {
                     hr=hr-12;
                    }
                    if(min>59)
                    {
                     hr=hr+1;
                      min=0;
                     }
                    if(ampm=='A'&&hr==1)
                    {
                    ampm='P';
                    }
                    if(ampm=='P'&&hr==1)
                    {
                    ampm='A';
                    }
             printf("%d:%.02d %cM\n",hr,min,ampm);
        }
    
    
        }
    for some reason when the hour switches to 1 it actually prints 13:00 then it goes to 1:01?

    Then the other problem is for some reason 'P' will switch to 'A' at hr=1 but 'A' will not switch to 'P' at hr=1 ?

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