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.
#include <iostream>
#include <string>
usingnamespace 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;
}
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.