Nina_Jenner Nina_Jenner - 1 month ago 20
C Question

what to use to check in between comparisons in c?

need to make a c program that will get the current time of pc and display if its traffic or not.

Traffic every : 7:00am - 10:00am && 17pm - 19pm

need help in time comparisons. im using if else.

if ((mytime > 7 && mytime < 10) && (mytime > 17 && mytime < 19))

Answer

Use || (OR) instead of && (AND). Or it will never work.

And to get the hour, do something like that:

#include <stdio.h>
#include <time.h>

int main() {
    time_t now = time(NULL);
    struct tm *now_tm = localtime(&now);
    int hour = now_tm->tm_hour;

    if ((hour > 7 && hour < 10) || (hour > 17 && hour < 19))
        printf("traffic\n");
    else
        printf("smooth\n");

    return 0;
}