0

I'm having some trouble with setting time out for cin>>.

After 5 seconds, if the user didn't input anything, skip this cin>> and do the next line.

I tried using threads but I can't skip this cin>>. The cin thread is still running until it got an input.

Pang
  • 9,564
  • 146
  • 81
  • 122
  • Possible duplicate of [Is it possible to set timeout for std::cin?](http://stackoverflow.com/questions/9053175/is-it-possible-to-set-timeout-for-stdcin) – Robert C. Holland May 04 '17 at 03:05

1 Answers1

-1

I believe this should work.

#include <iostream>
#include <ctime>

int main(){
    int a;
    unsigned t0=time(0);
    std::cin >>a;
    unsigned elapsed=time(0)-t0;
    std::cout <<elapsed<<std::endl;
}
Chris
  • 81
  • 10
  • It is not the correct answer. Your code just print the time that the user spends to enter an integer. – francas Aug 17 '17 at 08:13