You are trying to do something that is
platform-dependent with the same code on every platform. By definition that can't be done.
The simplest answer is to simply turn off echo.
On Windows
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <windows.h>
void echo( bool on = true )
{
DWORD mode;
HANDLE hConIn = GetStdHandle( STD_INPUT_HANDLE );
GetConsoleMode( hConIn, &mode );
mode = on
? (mode | ENABLE_ECHO_INPUT )
: (mode & ~(ENABLE_ECHO_INPUT));
SetConsoleMode( hConIn, mode );
}
|
On POSIX (Unix, Linux, etc)
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <termios.h>
#include <unistd.h>
void echo( bool on = true )
{
struct termios settings;
tcgetattr( STDIN_FILENO, &settings );
settings.c_lflag = on
? (settings.c_lflag | ECHO )
: (settings.c_lflag & ~(ECHO));
tcsetattr( STDIN_FILENO, TCSANOW, &settings );
}
|
You will have to link with one of
-lcurses or
-ltermios or
-lncurses ... whichever is appropriate for your system.
Now, you can turn echo on or off easily
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
using namespace std;
#include "platform-code.hpp"
int main()
{
string pwd;
cout << "Please enter a passcode> ";
echo( false );
getline( cin, pwd );
echo( true );
cout << "\nYour password is \"" << pwd << "\"\n";
return 0;
}
|
When you compile and link, compile the platform-dependent code that is appropriate for your platform: the windows stuff on windows and the POSIX stuff on Unix/Linux/whatever. If you have other platforms in mind, you will have to write code appropriate for that too. Then link the module into the final executable.
Hope this helps.
[edit] BTW, I didn't test any of this code -- errors may have occurred.