1

I have C++ code that look like this:

void Typewriter(string strOk) {    
    for (unsigned int x = 0; x < strOk.length(); x++) {
        cout << strOk[x];
        Sleep(50);

    }
    cout << endl;
}

int main() {
    Typewriter("OK, Hello. This is a first thingy. IDK.");
    Typewriter("This is the second line, also the second call to the function");
    cin.get();
    Typewriter("You pressed Enter! [Enter] next page");
    cin.get();
    Typewriter("Haha, no next page. Now, cya!");
    return 0;
}

It works just fine, except while cin.get() is waiting for an Enter keypress, the user can enter other characters and they will show up in the console.. It kind of acts like cin but executes the next statement when Enter is pressed. How do I restrict keyboard presses from being shown in the console and only react when Enter is pressed?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
Joseph
  • 1,003
  • 3
  • 11
  • 25
  • You will need to use platform or OS specific functions. Search StackOverflow for existing answers. Something like "c++ keyboard press" or "c++ input nonblocking". – Thomas Matthews Sep 05 '15 at 00:42
  • 2
    Q: How do I restrict keyboard presses from being shown in the console and only react when Enter is pressed? A: Your best bet is to consider using a library like [ncurses](http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) – paulsm4 Sep 05 '15 at 00:47
  • possible duplicate of [Hide user input on password prompt](http://stackoverflow.com/questions/6899025/hide-user-input-on-password-prompt) – NathanOliver Sep 05 '15 at 03:08

0 Answers0