Hangman Game

i have a text document with 10 words to guess. i have the code already read a random word from the file. then the user guesses a letter and it finds whether or not if the letter is in the word. my problem is how should i loop so it remembers the previous letter and outcome and builds on so it will complete the entire word. my teacher said this as a hint " I suggest you make a word of all blanks
then replace letters with the letters that are in the word to be guessed
you can use the position found by find to put the letter in the appropriate position." but how would i go about doing that any help would be greatly appreciated.

#include <iostream>
#include <fstream>
#include <time.h>



using namespace std;


void drawing();
void guessword(string word);



int main()
{
int number;
int count = 0;
int position;
string word;
char letter;

ifstream infile;
infile.open("words.txt");

srand(time(NULL)); //initializes the random number generator
while (count < 1)
{

number = rand()%10 +1; // rand returns a random integer from 0 to maxInt
count++;
}


for (count=0;count<number;count++)
getline (infile, word);




guessword(word);





cin.ignore(255,'\n');
cin.get();
return 0;
}









void guessword(string word)
{
char letter;
int position;

for (int i=0;i<5;i++)
{
cout << "What letter would you like to guess?";
cin >>letter;

position = word.find(letter);
if (position > word.length())
cout<<letter<< " is not in the word "<<endl;
else
{
cout<< letter << " is in the word"<<endl;
if(position==0)
cout<<letter<<" _ _ _"<<endl;
else if(position==1)
cout<<"_ "<<letter<<" _ _"<<endl;
else if(position==2)
cout<<"_ _ "<<letter<<" _"<<endl;
else if (position==3)
cout<<"_ _ _ "<<letter<<endl;
}




}




}



void drawing(int number)
{

switch(number)
{
case 1:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | " <<endl;
cout << "_|______________"<<endl;
break;
case 2:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \\ " <<endl;
cout << "_|______________"<<endl;
break;
case 3: cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \\ 0 " <<endl;
cout << "_|______________"<<endl;
break;
case 4:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \\ 0 /" <<endl;
cout << "_|______________"<<endl;
break;
case 5:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \\ 0 /" <<endl;
cout << " | |"<<endl;
cout << "_|______________"<<endl;
break;
case 6:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \\ 0 /" <<endl;
cout << " | |"<<endl;
cout << " | / "<<endl;
cout << "_|______________"<<endl;
break;
case 7:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \\ 0 /" <<endl;
cout << " | |"<<endl;
cout << " | / \\ "<<endl;
cout << "_|______________"<<endl;
}
}
Last edited on
First, include:

#include <string>

Try to create another string variable, called something like wordguessed and set it full of the amount of blanks for the actual word by going:

for (int i=0; i<word.length(); ++i) wordguessed.insert(i, " ");

Then when you find the "position" of a letter in the word as you already show in your code, assign it to your wordguessed string like this:

wordguessed[position]=word[position];

Then you can check to see if the wordguessed has been completely filled with the word by using an if statement:

if (wordguessed==word) cout << "You win!";

(more likely you will want to use the if statement in a way that gracefully breaks out of game-play and informs the user of the win)

~psault


Topic archived. No new replies allowed.