Thread: Example from book - time(NULL) doesn't work

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45

    Example from book - time(NULL) doesn't work

    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:
    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={{{,}}}
     */
    This is the piece of code that should make it wait:

    Code:
            /* This DOES NOT work. */
            do {
                i_Elapsed_Time = time(NULL);
            } while ((i_Elapsed_Time - i_Current_Time) < 3);
    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.

    Thanks in advance.
    Last edited by FernandoBasso; 10-30-2011 at 04:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. first time compiling with external .c and .h file, doesn't work
    By Adam Rinkleff in forum C Programming
    Replies: 17
    Last Post: 06-21-2011, 05:38 PM
  2. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  3. Replies: 3
    Last Post: 03-07-2003, 09:06 PM
  4. Time Delay doesn't work!
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-17-2001, 01:12 PM

Tags for this Thread