How to I hide text?

Image
Im trying to create a hangman game witch im successful at that but one thing. I can't figure out how to make input not show just for one string. so this is what I have
1
2
3
4
     cout << "Welcome to Hangman!\n";
     cout << "Enter a word: ";
     string word;
     cin >> word;

So when I run it says

Welcome to Hangman!
Enter a word: Hangman

I typed Hangman into the "Enter a word: " it shows the word I want the input to not show.

Sorry i put so much detail in this people usually say I need more so I listened

Thanks! :)
Last edited on
Image
The hiding of characters is an OS thing and doesn't happen by default.
Depending on wheher you are using *nix or windows, here are some examples I pinched from
codeguru forum:


WINDOWS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
    DWORD mode = 0;
    GetConsoleMode(hStdin, &mode);
    SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));

    string s;
    getline(cin, s);

    cout << s << endl;
    return 0;
}//main 



*NIX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>

using namespace std;

int main()
{
    termios oldt;
    tcgetattr(STDIN_FILENO, &oldt);
    termios newt = oldt;
    newt.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    string s;
    getline(cin, s);

    cout << s << endl;
    return 0;
}//main 
Image
Ok ill try that and then reply if it works or not Thanks :)
Image
It Works!
Image
Neither version cleans up after itself. Make sure to restore the terminal state before you terminate. (I would restore it immediately after getting the word.)

SetConsoleMode(hStdin, mode);

tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
Topic archived. No new replies allowed.