How to implement timeout for getline()? [duplicate]
问题 This question already has answers here : Is it possible to set timeout for std::cin? (2 answers) Closed 7 years ago . I want to read a string from command line through getline() in c++. For that I want to add a timer of 5 sec. If no string read, then the program will terminate. How can I do this? 回答1: How about: /* Wait 5 seconds. */ alarm(5); /* getline */ /* Cancel alarm. */ alarm(0); Alternatively you could use setitimer . As R. Martinho Fernandes requested: The function alarm arranges for