Making words not appear on the screen while the user types them

Okay so I am trying to figure out how to take the word that player one enters and make it not shown on the screen so that player 2 can't just read the word typed in and win every time. Any suggestions. Look at line 42 if you want to look at the code I have written for the player entering the word.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>
using namespace std;
//Robin Brust
//CS 161/Homework 5
//This is guessing a game where one player enters a word or phrase for the other
//person to guess. The game is played until one player guesses the word or runs 
//out of guesses.



void welcome()
{
     cout<<"Welcome to Robin's guessing game."<<endl;
     cout<<"The object of the game is to be the user with the"<<endl;
     cout<<"least points. Player 1 will enter a word that"<<endl; 
     cout<<"player 2 must guess. For every incorrect guess 1 point will"<<endl;
     cout<<"be added to player ones score. For every correct guess a point"<<endl;
     cout<<"will be added to player twos score. If you think you know the"<<endl;
     cout<<"solution you may enter it at anytime. Good luck"<<endl<<endl;  
     cin.get(); 
} 

string player_one()
{ 
    char player_one[20];
    cout<<"Player 1 please enter your name"<<endl;
    cin>>player_one;
    cin.get();
    return (player_one);
}


    
  
void game_play()
{
    char word[25];//Creates memory for the word that is to be guessed
    char blank[25];//creates blank spaces
    char guess;
    cout<<player_one()<<" please enter a word or phrase with 25 or less characters"<<endl;
    int counter=0; //simple counter
    cin.getline (word,25);
    strlen(word);
    cout<<"The word is "<<strlen(word)<<" characters long!"<<endl;
    int length=strlen(word); //stores the length of the word or phrase
    for (counter = 0; counter < length; counter++)//converts the letters to uppercase
        {		
           word[counter] = toupper(word[counter]);
       	}
    strcpy(blank,word);//Copies the word to the 'blank' array
    for (counter = 0; counter < length; counter++) 
    {			
           if (isalnum(word[counter])) blank[counter] = '_';	//converts characters to _'s to represent blanks 	
   	       else blank[counter] = word[counter]; 
    }
   	char player_two[20];
    cout<<"Player 2 please enter your name"<<endl;
    cin>>player_two;
    cin.get();
    
          //play game until the 'blank' puzzle becomes the 'right' answer 	
       do	{
           cout<<"The current blank puzzle is: "<<blank<<"."<<endl;	
       	   cout<<player_two<<" enter a guess."<<endl;		
           cin>>guess;		
           guess = toupper(guess);  				
           for (counter = 0; counter <= length; counter++) //check guess
           { 				
              if (guess == word[counter]) 
               { 				
              	blank[counter] = guess;		//fill in the puzzle with the letter 			
       	       } 		
           }			
                      
        }
        while (strcmp (word,blank) != 0);     	                   	
    cout<<"Winner!"<<endl;
   	cin.get();
}  
    

         	
bool play_again()
{
     char again;
     cout<<"Would you like to play again? y/n "<<endl;
     cin>>again;         
     cin.get();
     return (again == 'y' || again == 'Y');
}
    	
int main()
{
    welcome();
    do{
       game_play();
      }while (play_again());          
    return 0;
}
Last edited on
Why don't you make two global ints. first increases after line 79. the second wold increase when the player is wrong. To do that you may want to change line 78. Into something like
1
2
3
    bool different = strcmp(word, blank);
    if(different) another_players_score++;
}while(!different); 


Another thing. You should change the whole code structure. Now players wold have to reenter their names if they wanted to play again. Also here player1 always chooses the word and player2 tries to guess it. Try splitting your code into more functions such as enter_name, choose_word, guess_word or smething like that. It should make things easyer. Lastly, use more strings. You use only one of them. That's quite weird. That way you won't have to limit the length of the words.

Hope this helps
like hangman without the rope. neat.

if you can #include <conio.h> then you can use getch() to get one char at a time.

you won't be able to do fancy stuff like backspace unless you write the code for it.

you won't see the word you enter, so you may not catch a typo. the winning phrase may be a surprise to both players!
To enter stuff without the user seeing it you have to disable ECHO in the prompt.

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
31
32
33
34
35
36
37
38
39
40
41
42
// On Windows
#ifdef __WIN32__

#include <windows.h>

void echo( bool b )
{
  DWORD  mode;
  HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );
  GetConsoleMode( hstdin, &mode );

  if (b) mode |=   ENABLE_ECHO_INPUT;
  else   mode &= ~(ENABLE_ECHO_INPUT);

  SetConsoleMode( hstdin, mode );
}

// On Unix and Linux
#else

#include <unistd.h>
#include <termios.h>

void echo( bool b )
{
  static bool           needs_init = true;
  static struct termios initial_settings;
  struct termios         settings;

  if (needs_init)
  {
    tcgetattr( STDIN_FILENO, &initial_settings );
    needs_init = false;
  }
  settings = initial_settings;

  if (!b) settings.c_lflag &= ~(ECHO);

  tcsetattr( STDIN_FILENO, TCSANOW, &settings );
}

#endif  

Now you can use it easily enough:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
  string secret;
  cout << "Please enter your secret> " << flush;
  echo( false );
  getline( cin, secret );
  echo( true );

  cout << "\nYour secret is \"" << secret << "\".\n";

  return 0;
}

Hope this helps.

BTW, typos and errors may have occurred.
Duoas, alternatively on UNIX/Linux you can do as in this article: http://www.cplusplus.com/forum/articles/16139/

Perhaps Null could incorporate your echo function into that...
Thanks for all your help!!
Curses works on every (useful) platform. However, it also takes total control of your terminal, and is not very friendly with C++ STL streams.
It's also really annoying to use... I've tried it a few times and could never get it to do what I wanted.
Topic archived. No new replies allowed.