Urda
Urda

Reputation: 5411

How to prevent console output from cin

How does one prevent cin from printing to a console screen in C++? Given this simple program:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World..." << endl;

    cin.clear();
    cout << "Press ENTER to Continue..." << endl;
    cin.ignore();

    exit(0);
}

So if a user jams away at the keyboard, nothing will happen until ENTER is pressed. This currently works great, but cin dumps the keypresses to the console. How do I prevent this behavior?


Edit: I'm working in Visual Studio 2010, and I ask this simple question because I want something not platform specific.

Upvotes: 0

Views: 2563

Answers (3)

Talya
Talya

Reputation: 19347

It can't be done. C++, as a language specification, isn't about your keyboard—that's purely a decision of the platform, and unfortunately the various platforms didn't (even remotely) come close to agreeing on a specification for anything vaguely terminal-related.

Better not to worry about or rely on it.

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283793

On Windows, you need SetConsoleMode.

There is no standard platform independent way, although of course you can write your own disable_echo() function and use #if _WIN32 and #if __LINUX__ to provide platform-specific implementations of a platform-independent interface.

Upvotes: 5

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799240

You need to use the termios(3) functions to toggle the ECHO mode.

Upvotes: 0

Related Questions