?
Solved

How do I timeout when waiting for user input?

Posted on 2005-04-08
2
Medium Priority
?
2,073 Views
Last Modified: 2008-02-01
Hi,

I'd like to ask the user a question on a console application, but I'd like a default ansnwer which will get executed if there in no answer.  I will be using cin and cout.

Thanks,
Bob
0
Comment
Question by:ba272
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 13740765
I think you can use cin.peek() function to fetch if a character has ben input before calling getline()

Something like

#define TIMEOUT 30  // seconds

bool isTimeout = true;
time_t t = time(null) + TIMEOUT;   // will require time.h
while (time(null)<t) {
    if (cin.peek()!=EOF) {
        isTimeout = false;
        break;
    }
}

if (!isTimeout) {
     // use getline or other input method
} else {
     // Print error message
}
0
 

Author Comment

by:ba272
ID: 13741158
Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question