2 Replies - 293 Views - Last Post: 21 March 2013 - 11:06 PM Rate Topic: -----

#1 MegaTerminatorz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-October 12

Time Limit for Response, where the the program will immediately exit.

Posted 21 March 2013 - 09:18 PM

Hello. I have been searching for an answer to this exact question for the last 40 minutes with Google, but I haven't been able to find an explanation that I can understand. It would definitely be nice if I could be given a few references or an explanation of how to do this.

I would like to create a program that imposes a time limit user, so the user will need to input any answer before 10 seconds. If the user doesn't enter a variable before the time limit, I'd like to interrupt the cin>>variable function ( or possibly something else that works just as well), when the user takes more than 10 seconds, so I can display a message, telling the user that he took too long.

#include <time.h>
#include <iostream>

using namespace std;

unsigned int difference ( unsigned int &start_time, unsigned int &end_time )
{
    end_time = clock();
    return ( ( end_time - start_time )  /1000 );
}


int main ()
{
    unsigned int start_time = clock();
    unsigned int end_time;
    int variable;

    while ( difference ( start_time, end_time) < 10 )
    {
        cout<< difference ( start_time, end_time) << endl;
        cin >> variable;
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Time Limit for Response, where the the program will immediately exit.

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Time Limit for Response, where the the program will immediately exit.

Posted 21 March 2013 - 10:40 PM

What is called non-blocking input is platform dependent.

If you are using Windows one option is _kbhit.

You would need to read each character of the input.
Was This Post Helpful? 1
  • +
  • -

#3 MegaTerminatorz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-October 12

Re: Time Limit for Response, where the the program will immediately exit.

Posted 21 March 2013 - 11:06 PM

View Post#define, on 21 March 2013 - 10:40 PM, said:

What is called non-blocking input is platform dependent.

If you are using Windows one option is _kbhit.

You would need to read each character of the input.

Thank you. I'll be sure to do that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1