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.
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.
You need to use the termios(3)
functions to toggle the ECHO
mode.
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.
精彩评论