0

I have this simple program in c that I'm supposed to add a countdown timer. The program must print the amount of time left for the user (player) every second.

int main()
{
    int num, random_genNo;
    int count = 0;
    random_genNo = rand()%100;
    while(1) {
        count++;
        printf("deviner le nombre entre 0 et 100\n");
        scanf("%d",&num);
        if(random_genNo==num) {
            printf("felicitation, vous avez devinez le numero correcte.\n");
            printf("nombre de coups  %d", count);
            break;
        } else if(random_genNo<num) {
            printf("numero a deviner est inferieur a votre numero, ressayer...\n");
        } else if(random_genNo>num) {
            printf("numero a deviner est superieur a votre numero, ressayer...\n");
        }
    }
return 0;
}

I thought about transforming the whole program to a state machine with a uchar Counter decrementing every second but i faced a lot of problems. So I created a state machine that only does the countdown timer. The rest of the program is in the main but got the same issue.

I think it may have a better way to approach this. Any idea?

TheArchitect
  • 1,160
  • 4
  • 15
  • 26
aouainia
  • 21
  • 5
  • You might like to look into the `alert()` function's documentation/man-page. – alk Mar 07 '20 at 11:32
  • 1
    There are several possibilities. One of them is to use poll with a timeout, see my answer to a similar question here: https://stackoverflow.com/a/58143550 – Stephan Schlecht Mar 08 '20 at 08:46

0 Answers0