time limit for user input

This is a discussion on time limit for user input within the C++ Programming forums, part of the General Programming Boards category; is there a way to give a user let's say 10 seconds to enter something in? I looked at what's ...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    time limit for user input

    is there a way to give a user let's say 10 seconds to enter something in?

    I looked at what's in ctime and all i can do right now is just pause things for x amount of seconds.

    if like to have something where a loop starts counting down and then breaks once the user enters something. If they don't answer and the timer runs out, I can record that as a bool or something.

    any ideas???
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Can't be done easily.

    You will have to use third party libraries, and I am not sure if you can do it even then. On the console at least.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    As you seem to be on Linux, look at ncurses.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    This question already been asked in the past. I gave this code (C) as an example, once:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <time.h>
    #include <signal.h>
    #include <assert.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    
    const unsigned int MAX_TIME = 5;
    
    enum
    {
        OK = 0,
        TOO_SLOW
    } result;
    
    void sig_handler(int sig_num)
    {
        if (sig_num == SIGCHLD)
        {
            result = OK;
        }
    
        else if (sig_num == SIGALRM)
        {
            result = TOO_SLOW;
        }
    }
    
    int main()
    {
        pid_t chld_process;
    
        struct sigaction sa;
    
        int shm_id;
        void *data;
    
        assert((shm_id = shmget(IPC_PRIVATE, sizeof(int), 0600 | IPC_CREAT)) != -1);
        assert((data = shmat(shm_id, NULL, 0)) != (void*) -1);
    
        sa.sa_handler = sig_handler;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = 0;
        assert(sigaction(SIGALRM, &sa, NULL) != -1);
        assert(sigaction(SIGCHLD, &sa, NULL) != -1);
    
        printf("Enter a number: ");
        fflush(stdout);
    
        assert((chld_process = fork()) != -1);
        if (chld_process == 0)
        {
            scanf("%d", (int*) data);
            shmdt(data);
            exit(EXIT_SUCCESS);
        }
    
        alarm(MAX_TIME);
        pause();
        alarm(0);
    
        if (result == OK)
        {
            printf("Thanks for %d\n", * (int*) data);
        }
    
        else
        {
            kill(chld_process, SIGINT);
            printf("\nToo slow... .\n");
        }
    
        shmdt(data);
    
        return 0;
    }
    I'm quite sure it's not the best way to do it, I did it that way because I though it could work and indeed, it worked. The code ask for a number, and you have only MAX_TIME seconds to enter it. It uses signals, shared memory segments and process creation.
    I hate real numbers.

  5. #5
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by Salem View Post
    As you seem to be on Linux, look at ncurses.
    i may throw together a ncurses example in a little while.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 08:51 PM
  3. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM
  4. Input looping......it appears once extra time!?
    By siumui in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 10:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21