Thread: Three similar while loops, but only one works fully as intended

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    1

    Three similar while loops, but only one works fully as intended

    Hey, I've been working on an alarm clock that uses three while loops. Problem is that only the last one works properly. The program takes in two inputs, current time and alarm time (both in HHMMSS). Those get split into hours, minutes and seconds, which the loops then use.

    Correct output:

    23:59:58
    23:59:59
    00:00:00
    00:00:01
    00:00:02
    ALARM

    Incorrect output:

    20:14:55
    20:14:56
    20:14:57
    20:14:58
    20:14:59
    20:15:00

    Here're my three while loops:

    Code:
    while(present_hour != alarm_hour){if(present_hour == alarm_hour && present_minute == alarm_minute && present_second == alarm_second){
                printf("ALARM");
            }
            else{
                if(present_second < 60){
                    present_second++;
                    
                    if(present_second == 60) {
                        present_second = 0;
                        present_minute++;
                    }
                    if(present_minute == 60){
                        present_minute = 0 ;
                        present_hour++;
                    }
                    if(present_hour == 24){
                        present_hour = 0;
                    }
                    printf("%02d-%02d-%02d \n", present_hour, present_minute, present_second);
                        
                }
            }
        }
        while(present_minute != alarm_minute){
            if(present_hour == alarm_hour && present_minute == alarm_minute && present_second == alarm_second){
                printf("ALARM");
            }
            else{
                if(present_second < 60){
                    present_second++;
                }
                    
                    if(present_second == 60){
                        present_second = 0;
                        present_minute++;
                    }
                    printf("%02d_%02d_%02d \n", present_hour, present_minute, present_second);
            }    
        }
        while(present_second != alarm_second){
            present_second++;
            if(present_hour == alarm_hour && present_minute == alarm_minute && present_second == alarm_second){
                printf("ALARM");
            }
            else{
            printf("%02d:%02d:%02d \n", present_hour, present_minute, present_second);
            }
        }
    I really don't get why only the last loop prints "ALARM" instead of the alarm time. Since "ALARM" being printed requires the if statement being true, I could see that being the problem. But if it works in the last loop, why doesn't it work for the other two?

    I've been staring at these loops for quite some time, to no avail. Any pointers or hints would be greatly appreciated! I hope I've explained myself well enough, but feel free to ask if something is weird or confusing.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Your indentation does not match the logic. Maybe that's the problem. At any rate, it would be simpler like this:
    Code:
        while (hour != alarm_hour || minute != alarm_minute || second != alarm_second) {
            if (++second == 60) {
                second = 0;
                if (++minute == 60) {
                    minute = 0 ;
                    if (++hour == 24)
                        hour = 0;
                }
            }
            printf("%02d-%02d-%02d \n", hour, minute, second);
        }
        printf("ALARM\n");
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    I don't fully understand your complaint, because your "error" example looks fine to me -- what should it be doing?

    Regardless, I think you are seeing the behavior that you are, because of the guards in the while loops. Your first loop is guarded on present_hour != alarm_hour, which is fine. But as soon as the hours match, that loop will exit, right?

    So if the alarm time happens to be exactly HH:00:00, then your first loop might catch it. But otherwise, if the alarm time is HH:MM:00 or HH:MM:SS (where HH, MM, SS are non-zero), then the first loop will reach alarm_hour == present_hour and that's the end of that loop.

    With that said ...

    As john.c points out, you don't need three loops in sequence. If you are going to use three loops, they should be nested one within the next. But you don't need that since you can just do cascading updates with a few if statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf not functioning as intended?
    By Jonnyb in forum C Programming
    Replies: 2
    Last Post: 11-29-2010, 02:40 PM
  2. Not working as intended
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2008, 11:10 AM
  3. Cgi Query (no pun intended)
    By Sebastiani in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-29-2003, 08:22 AM
  4. wut is the point of pointers (no pun intended)
    By Geo-Fry in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2003, 12:13 PM

Tags for this Thread