Help with Hangman Game

Newbie here, need help with my code for Hangman.
Here is my code

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;
bool isWordGuessed(string secretWord, string lettersGuessed);
string getGuessedWord(string secretWord, string lettersGuessed);
string getAvailableLetters(string lettersGuessed);
 
const int MAX_NUMS = 100;
const string LETTERS = "abcdefghijklmnopqrstuvwxyz";
int main()
{
	string oneWord, words[MAX_NUMS];
	string secretWord;
	string lettersGuessed = "";
	string availLetters;
	string guessedWord;
	bool WordGuessed;
	char letter;
	int count = 0;
	int numberOfGuessess = 8;
	
    ifstream myFile;
	myFile.open("words.txt");
	
    if (!myFile)
	{
		cout << "Error: Unable to open file for input" << endl;
		return 0;
	}

	myFile >> oneWord;
	
    while (!myFile.eof())
	{
		words[count] = oneWord;
        count++;
        myFile >> oneWord;
	}
    
    cout << "Welcome to the game Hangman !" << endl;
      
      srand(time(0));
      secretWord = words[rand() % count];
      cout << "Loading word list from file...." << endl;
      cout << count << " words loaded" << endl;
      cout << "I am thinking of a " << secretWord.length() << " letter word." << endl; 
      cout <<"The word is " << secretWord << endl;
      cout << "----------------------------------" << endl;
      while(numberOfGuessess > 0)
      {
      availLetters = getAvailableLetters(lettersGuessed);
      
      cout << "You have " << numberOfGuessess << " guessess left" << endl;
      cout << "Available Letters " << availLetters << endl;
      cout << "Please guess a letter: ";
      cin >> letter;
      
      numberOfGuessess = numberOfGuessess - 1;
      
      if(lettersGuessed.find(letter) == string::npos)
      lettersGuessed += letter;
 
      
      WordGuessed = isWordGuessed(secretWord,lettersGuessed);
      guessedWord = getGuessedWord(secretWord,lettersGuessed);
      
      cout << guessedWord << endl;
      cout << "--------------------------------------" << endl;
      }
      	
	myFile.close();
    system("pause");
	return 0;
}

bool isWordGuessed(string secretWord, string lettersGuessed)
{
     int i;
     for(i = 0; i < secretWord.length(); i++)
     {
           if(secretWord.find(lettersGuessed) == string::npos)
           return false;
           
           else 
           return true;
     }
}

string getGuessedWord(string secretWord, string lettersGuessed)
{
       string word = " ";
	   int i;
       for(i = 0; i < secretWord.length(); i++)
       {
             if(secretWord.find(i) != string::npos)
             word += i;
             else 
             word += " _ ";
       }
       return word;
}

string getAvailableLetters(string lettersGuessed)
{
       string availLetters = " ";
       int i;
       for (i = 0; i < LETTERS.length(); i++)
       if (lettersGuessed.find (LETTERS[i]) == string ::npos)
       availLetters += LETTERS[i];
       return availLetters;
}

Last edited on
use the <> button between your code next time, so its all pretty and easy to understand and the guys here who are best at helping know you had the kind of scrupels that would have had you read the boring 'read before posting thing'...

-- whats the error?
Last edited on
Like what do you mean use <> between my code. Where exactly?
I'm sorry it's my first time using this website.

The error is the output. The letters of the alphabet are suppose to disappear after I enter a letter. And if the letter is right, it's suppose to replace the _ with the letter. If it is wrong then it is suppose to display a _ while subtracting one form your guessess
Last edited on
Highlight all your code then click the "<>" button under Format: to the right of the text box. It just makes the code easier to read.
Ok did it
Last edited on
you could create a display function in a loop and use system ("cls"); or better practice make a function that just skips ten or so lines

Its the traditional game loop, you see the display you enter the input, and it updates the display, this way your _ string should be initialized to something else and tehre for displayed as such
I'm trying really hard, but I don't really understand what you're saying
I've u[dated it and made some adjusments but it's still not doing what I want it to
Did you update the code in your original post so we can see the changes or do you mean to make us guess what it's not doing ?
Topic archived. No new replies allowed.