C variable reading with timeout

1

Does anyone know how to limit the time the user can enter a value?

Example:

My program prints a value on the screen and the user has 4 seconds to enter and enter enter, if what he typed is = to the printed value, the value changes and counts a point, otherwise the value changes and counts -1 point.

    
asked by anonymous 29.06.2015 / 16:40

1 answer

1

Update

I noticed what the pmg said and went to research better on the subject, I found some interesting things, such as this code that works on linux, in addition the author has referred to a library called NCurses , and was also referred to by pmg .

I also found an OS issue where they used the select function, though I thought about using then I looked again and found a code that solved the problem.

#include <stdio.h>
#include <windows.h> 

#define TIMEOUT 4000 //4 segundos em milisegundos

void getInput(LPVOID param);


int main() {

    DWORD myThreadID;
    HANDLE myThread;
    int userInput = 0;

    myThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)getInput, &userInput ,0, &myThreadID);

    WaitForMultipleObjects(1, &myThread, TRUE, TIMEOUT);

    CloseHandle(myThread);

    if(userInput != 0){
        printf("\nValor %d", userInput);
    }else
       printf("\nO tempo limite foi excedido.");

    return 0;
}
void getInput(LPVOID param){
    printf("Insira o valor: ");
    scanf("%d",(int)&param);
}

In any case, I will keep the old solutions I presented.

Using Unix Timestamp

We can measure the time difference using Unix Timestamp, the idea is very simple, we measure the start and then measure the end.

Example:

#include <stdio.h>
#include <time.h>

int main(void) {

    int valor; //variavel que guarda o "valor"
    printf("Insira um valor: "); 

    time_t _start = time(NULL); //medimos o tempo no instante inicial, ou seja, antes da execução do scanf.

    scanf("%d", &valor);

    time_t _end = time(NULL); //medimos o tempo no instante final, ou seja, depois da execuação do scanf.

    if((_end - _start) >= 4) //calculamos a diferença entre o final e o inicial, se o valor for superior a 4 então o tempo limite foi excedido.
    {
        printf("\nFoi excedido o tempo limite..");
    } else {
        printf("valor %d", valor);
    }
    return 0;
}   

Using the clock

The code is the same what changes is the use of the function clock and return, instead of returning a value in seconds this returns in milliseconds, hence the multiplication by 1000 is necessary.

int main(void) {

    int valor; //variavel que guarda o "valor"
    printf("Insira um valor: "); 

    clock_t _start = clock(); //medimos o tempo no instante inicial, ou seja, antes da execução do scanf.

    scanf("%d", &valor);

    clock_t _end = clock(); //medimos o tempo no instante final, ou seja, depois da execuação do scanf.


    if((_end - _start) >= (4*1000) //calculamos a diferença entre o final e o inicial, se o valor for superior a 4000 então o tempo limite foi excedido.
    {
        printf("\nFoi excedido o tempo limite..");
    } else {
        printf("valor %d", valor);
    }
    return 0;
}

Using QueryPerformanceCounter and QueryPerformanceFrequency

Thank you for deepmax for posting a time code in the question: Measure execution time in C (on Windows)

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(void) {

    LARGE_INTEGER _frequency;
    LARGE_INTEGER _start;
    LARGE_INTEGER _end;
    double interval;

    int valor; //variavel que guarda o "valor"
    printf("Insira um valor: "); 

    QueryPerformanceFrequency(&_frequency);
    QueryPerformanceCounter(&_start); //medimos o tempo no instante inicial, ou seja, antes da execução do scanf.

    scanf("%d", &valor);

    QueryPerformanceCounter(&_end); //medimos o tempo no instante final, ou seja, depois da execuação do scanf.

    interval = (double) (_end.QuadPart - _start.QuadPart) / _frequency.QuadPart; //obtemos o intrevalo de tempo atravês da diferença entre o final e o inicial e a divisão da frequencia.


    if(interval >= 4.0) //verificamos se o intrevalo de tempo é superior a 4.0
    {
        printf("\nFoi excedido o tempo limite..");
    } else {
        printf("valor %d", valor);
    }
    return 0;
}

I hope these examples give you a basic idea of how to measure time and how to make a time limitation.

    
29.06.2015 / 18:10