I am trying to build a traffic light simulator which requires that I print green
for the first 8 seconds at 1 second intervals, yellow
for the next 4 seconds at 1 second intervals and red
for the last 8 seconds at 1 second intervals. How can I use time.h
to implement this in C?
This is my attempt, but I only get an output that prints green
nonstop at intervals which are not 1 second long.
// Traffic light simul`ator
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t start, end;
double elapsed;
time(&start); /* start the timer */
do {
time(&end);
elapsed = difftime(end, start);
if (elapsed )
{
printf("green");
}
} while(elapsed < 9);
}