entering information

hi,
I want to know that is it possible to ask a user to enter a value or no. and store it in the variable without echoing it on the screen.

1
2
cout<<"enter a no\n";
cin>>no;

where no is variable ,normally it will echo on screen while entering, I don't want it to be echoed on the screen at the same time want to save it in the variable....

thank you
It is displayed on the screen because this is how the console you are using works. There are different consoles, just as there are different games, but the main thing to consider is that the console should not be used the way you want to use it.
a modified version from http://www.cplusplus.com/forum/general/12256/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#if defined( _WIN32 )
#include <windows.h>
#elif defined( __gnu_linux__ )
#include <termios.h>
#include <unistd.h>
#endif

enum Echo_status { OFF = 0, ON = 1 };

#if defined( _WIN32 )
void echo( Echo_status echo_status )
{
    HANDLE hstdIn = GetStdHandle( STD_INPUT_HANDLE );
    DWORD mode = 0x0;
    GetConsoleMode( hstdIn, &mode );

    echo_status ? SetConsoleMode( hstdIn, mode )
       : SetConsoleMode( hstdIn, mode & ( ~ENABLE_ECHO_INPUT ) ) ;
}
#elif defined( __gnu_linux__ )
void echo( Echo_status echo_status )
{
    termios term;
    tcgetattr( STDIN_FILENO, &term );

    echo_status ? term.c_lflag |= ECHO : term.c_lflag &= ~ECHO ;

    tcsetattr( STDIN_FILENO, TCSANOW, &term );
}
#endif 


Although i haven't tested yet the _WIN32 version :D

basically you'll do something like :
1
2
3
4
cout << "Enter something: ";
::echo( OFF );
cin >> var;
::echo( ON );


EDIT
The _WIN32 one should be :
1
2
    echo_status ? SetConsoleMode( hstdIn, mode | ENABLE_ECHO_INPUT )
       : SetConsoleMode( hstdIn, mode & ( ~ENABLE_ECHO_INPUT ) ) ;
Last edited on
Topic archived. No new replies allowed.