jeudi 21 septembre 2017

why doesn't my program print the correct time?

Basically, this program is an "alarm clock". You input one present time, HHMMSS and one alarm time, HHMMSS. Then the program is supposed to print out the updated time in a HH:MM:SS format until it reaches the alarmtime, then it prints !ALARM!. The program works when alarmtime and present time are less the 60 seconds apart within the same minute (e.g present_time=223926 alarm_time=223948) but not when when the alarm time has different minutes or hours (e.g. present_time=235950 alarm_time=000109) What do you think the problem is? I've tried to fix this problem by saying that everytime seconds, minutes or hours reaches 60 it is set back to 0 but it doesn't seem to work.

My code:

#include "stdio.h"

int find_hour(int a);               // function prototypes
int find_min (int a);
int find_second(int a);


int main()
{
    int present_time,present_hour,alarm_time,alarm_hour,present_second,alarm_second,alarm_min,present_min;
    scanf("%d",&present_time);
    scanf("%d",&alarm_time);
    present_hour = find_hour(present_time);                 // function calls
    present_min = find_min(present_time);
    present_second = find_second(present_time);
    alarm_hour = find_hour(alarm_time);             
    alarm_min = find_min(alarm_time);
    alarm_second = find_second(alarm_time);

if(present_time==alarm_time)
{
    printf("!ALARM!");
}

while(present_hour!=alarm_hour || present_min!=alarm_min || present_second!=alarm_second)
{
     ++present_second;
    if(present_second==60)
        {
       int present_second=0;
        ++present_min;
        if(present_min==60)
            {
           int present_min=0;
            ++present_hour;
            if(present_hour==24)
                {
               int present_hour=0;
                present_min==0;
                present_second==0;
                }
            }
        }
    if(present_hour==alarm_hour && present_min==alarm_min && present_second==alarm_second)
        {
    printf("!ALARM!");
}
    if (present_hour!=alarm_hour || present_min!=alarm_min || present_second!=alarm_second)
    {
        printf("%d:%d:%d\n",present_hour,present_min,present_second);
    }
}


return 0;
}

int find_hour(int a)        // functions extracting hour,min,sec from time  
{
    int present_hour;
    present_hour = a/10000;
    return present_hour;                  
}

int find_min(int a)
{
    int present_min,b;
    b = a/100;
    present_min = b%100;
    return present_min;
}

int find_second(int a)
{
    int present_second;
    present_second = a%100;
    return present_second;
}

Aucun commentaire:

Enregistrer un commentaire