I'm reading a book of Micheal Vine, "C For The Absolute Beginner" (because I am an absolute beginner, actually) and there is this piece of code, which is an example from the book, but it doesn't work.
It is a game where you have to memorize numbers and be able to remember them. It should display the numbers for a while and then clear the screen. The problem is that it doesn't wait for a while. It shows the numbers and clears the screen right away.
Here's the entire code:
This is the piece of code that should make it wait:Code:#include <stdio.h> #include <stdlib.h> int main(void) { /* Variable definitions. {{{ */ char c_Yes_No = ''; int i_Resp1 = 0; int i_Resp2 = 0; int i_Resp3 = 0; int i_Elapsed_Time = 0; int i_Current_Time = 0; int i_Random_Number = 0; int i_1 = 0; int i_2 = 0; int i_3 = 0; int i_Counter = 0; /* Variable definitions end }}} */ srand(time(NULL)); printf(" Jogar o jogo da concentração? (s, n)"); scanf("%c", &c_Yes_No); if (c_Yes_No == 's' || c_Yes_No == 'S') { /* {{{ */ i_1 = rand() % 100; i_2 = rand() % 100; i_3 = rand() % 100; printf("Se concentre nos três números."); printf("%d %d %d", i_1, i_2, i_3); /* This DOES NOT work. */ do { i_Elapsed_Time = time(NULL); } while ((i_Elapsed_Time - i_Current_Time) < 3); /* This work. (I tried this myself. It is not from the book. */ /* sleep(3); */ system("clear"); printf("Digite cada número separado por um espaço: "); scanf("%d %d %d", &i_Resp1, &i_Resp2, &i_Resp3); if (i_1 == i_Resp1 && i_2 == i_Resp2 && i_3 == i_Resp3) { /* {{{ */ printf("Parabéns! Acertou todos."); } else { printf("Os números corretos eram %d, %d, e %d", i_1, i_2, i_3); } /* End child if. }}} */ } /* End 'if'. }}} */ return 0; } /* End of main() function. */ /* * vim:foldmethod=marker foldmarker={{{,}}} */
Of course the 'sleep()' function works. However, I would very much like to understand the piece of code with time(NULL) as shown in the book and if is supposed to work, why it doesn't.Code:/* This DOES NOT work. */ do { i_Elapsed_Time = time(NULL); } while ((i_Elapsed_Time - i_Current_Time) < 3);
Thanks in advance.



LinkBack URL
About LinkBacks



