0

this is my first question here, so please feel free to give any advice about my style.

I'm new to programming in C++. I'm using Windows. My problem is the following: I need a way to wait for an input for a limited time, so I can't use cin or _getch() or similar functions.

So my question is: is it possible to force the action of taking an input? I mean, even if the user typed not a single character, to retrieve an empty input of any kind (I guess an empty char array). That way I could do something like this:

int main(){
    cout << "Enter a 0 when you wish to stop the program..." << endl;
    int stop = 0;
    char buffer[20];
    while(stop==0){

        //main task of the loop

        //force to store the input inside buffer[], even if no input
        if(buffer=="0"){
            stop = 1;
        }
    }

By the way, the questions that I read related with this topic are a little bit old. If there is a simpler way to wait a certain amount of time for an user input, forget about that code and forcing the entry of data. Thank you very much for your time and forgive my inexperience.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Silverman
  • 299
  • 1
  • 10

1 Answers1

0

On many systems, you can use kbhit() to see if there is a character in the input buffer, and if there is, then use fetch() to read it. kbit() is not a standard across all C++ implementations, so check your system's documentation. There is a little more information here.

Community
  • 1
  • 1
Logicrat
  • 4,438
  • 16
  • 22