Thread: How to exit out of scanf after a certain amount of time

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    16

    How to exit out of scanf after a certain amount of time

    Hi all, I'm trying to make a very simple reaction game where a random number flickers on the screen for a fraction of a second and the user has to then enter the number before another comes on the screen after a random amount of time. However I dont know how i would make it so that the user cannot enter anything after a certain amount of time has passed, below is my code, could someone give me a hand? Also FYI, clock_start is at 5100 because by the time the program actually gets to scanning in the first number the time is at an absolute minimum of 5050 milliseconds however obviously this is an impossible number to reach due to processing, my machine clocks in at 5080.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <windows.h>
    
    
    int main(void)
    {
        int i;
        int random_number;
        int input, score = 0;
        int clock_start = 5100;
        
        system("cls");
        
        printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        printf("\t\t\t       WELCOME TO REACTION\n");
        
        Sleep(2000);
        
        for(i=3;i>0;i--)
        {
            system("cls");
            printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            printf("\t\t\t\t     %d", i);
        
            Sleep(1000);
        }
        
        srand(time(0));
        
        for(i=0;i<10;i++)
        {
            random_number = rand() % 1000;
            
            system("cls");
            printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            printf("\t\t\t\t    %d\n", random_number);
            
            Sleep(50);
    
    
            system("cls");
    
    
            printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            printf("\t\t\t\t    ");    
    
    
            clock();
    
    
            //this is the part im not getting
            ///////////////////
            if(clock() < clock_start + 3000)
            {    
                scanf("%d", &input);
            }
                
            else if(clock () > clock_start + 3000)
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                printf("\t\t\t\tTOO LATE!");
                
                Sleep(1000);
            }
            //////////////////////
            
            system("cls");
            
            Sleep(1000);
            
            if(input == random_number)
            {
                score++;
            }
        }
        
        printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        printf("\t\t\t\tYour overall score is %d", score);
        printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    }
    Last edited by Digital Android; 10-25-2014 at 02:25 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no way in standard C to force scanf() to time out, just as it is not possible in standard C to read keystrokes directly (without being forced to wait for the user to hit the enter key).

    Given that you are using non-standard functions (Sleep() is win32 API specific, not standard C) I assume a windows-specific solution would suffice.

    Have a look at this link. The windows-specific solution there is not a complete solution to your problem, but could provide a starting point.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    If you are going to do the timing with delays (the sleep function), then you need to read input with a function that does not block. scanf() blocks; ie, it does not return until something is input.
    So if using delays, you want a function that checks for input and returns immediately. Something like getch() could be used, but requires additional code to extract an input value from the sequence of characters.

    scanf() would work if you had an independant timer that ran continuously; you start the timer and wait for input; when input is received, you check the timer and see if it is less than the maximum allowed.

    -

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You probably want to look into curses (cross platform terminal library) or win32 api (Windows only). As mentioned curses is not standard but it is pretty ubiquitous. For example on Linux and MacOS there is Ncurses (maybe on iOS as well?). Pdcurses works well on Windows. With this library you get to implement delays, non-blocking input and the kinds of things you seem to be trying like clearing the screen and printing text on certain positions on the terminal. If you want to learn curses you can go here

    NCURSES Programming HOWTO

    And then read this (it is directed at Python audience but notice how the concepts and library functions are identical to the C library): https://docs.python.org/2/howto/curses.html

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    I should correct what I said above:

    "Something like getch() could be used"

    getch() does block and wouldn't work by itself.

    kbhit() does not block, and would work. It only checks whether or not a character was entered and returns immediately. You then use getch() to extract it.

    Both are not standard, but if you really need them, include conio.h

    -

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You could use _dup2() to create a copy of the stdin stream, then fire up a second thread to try to read from it. In the main thread, sleep the required time and then close the duplicated stream handle. This will cause the call to fread() in the other thread to return with EOF.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by megafiddle View Post
    I should correct what I said above:

    "Something like getch() could be used"

    getch() does block and wouldn't work by itself.

    kbhit() does not block, and would work. It only checks whether or not a character was entered and returns immediately. You then use getch() to extract it.

    Both are not standard, but if you really need them, include conio.h

    -
    You could poll kbhit() say 1000000 times (which would take some time) - And if it does not detect an input in that time, it has timed out.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int getInput(unsigned int timeout)
    {
        for (; timeout != 0; timeout--)
        {
            if (kbhit())
            {
                return getch();
            }
        }
    
        return EOF;
    }
    int main(void)
    {
        int ch;
    
        if ((ch = getInput(100000)) != EOF)
        {
            printf("Input Found: %c", ch);
        }
        else
        {
            printf("No input found\n");
        }
    
        return EXIT_SUCCESS;
    }
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to scanf a amount of integers ot an array
    By orid in forum C Programming
    Replies: 7
    Last Post: 09-02-2011, 01:09 PM
  2. scanf() doesn't execute in its 2nd time use
    By dnayak382 in forum C Programming
    Replies: 8
    Last Post: 08-16-2011, 09:56 AM
  3. Handling scanf/cin at executin time of program
    By chandru in forum C Programming
    Replies: 1
    Last Post: 04-10-2011, 12:15 AM
  4. Replies: 17
    Last Post: 07-20-2007, 09:25 PM