C++ hangman project doesn't work correctly

my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int instructions();
void manual();
void file(char*);
int letterFill(char, char*, char*);
void Unknown(char*, char*);
const int MAX_LENGTH = 10;
void main()
{
bool done = false;
char word[MAX_LENGTH];
char unknown[MAX_LENGTH];
char letter;
char name[MAX_LENGTH];
int wrong_guesses = 0;
int MAX_TRIES;
char ans;


while (!done)
{
switch (instructions())
{
case 1:
{
manual();
break;
}
case 2:
{
file(word);
break;
}
}

cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
cin >> MAX_TRIES;
Unknown(word, unknown);
cout << endl << endl << "HANGMAN";
cout << endl << endl << "Each letter is represented by a star." << endl;
cout << "You have " << MAX_TRIES << " tries left.";
cout << "ENTER GUESS WHEN READY: ";
cin >> letter;

while (letter != 'N' && letter != 'n')
{

while (wrong_guesses < MAX_TRIES)
{


cout << unknown << endl;
cout << "Guess a letter: " << flush;
cin >> letter;


if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "You got it wrong! You lose a guess" << endl;
wrong_guesses++;
}
else
{
cout << endl << "Pfft, you got lucky" << endl;
}

cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;


if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "You got it!" << endl;
exit(0);
}
cout << "You've been hanged." << endl;
cout << "The word was : " << word << endl;
}
}

cout << "Try again? (y/n): " << flush;
cin >> ans;
if (ans == 'y' || ans == 'Y')
done = true;
else
done = false;
}
system("pause");
}

int instructions()
{
int select = 0;
cout << endl << "HANGMAN" << endl << endl;
cout << " PROGRAM MENU" << endl;
cout << " Select option 1 or 2" << endl << endl;
cout << " 1. INPUT WORD MANUALLY" << endl;
cout << " 2. PLAY AGAINST THE COMPUTER" << endl;
cout << " 3. EXIT PROGRAM BY INPUTING: N or n" << endl << endl;
cin >> select;
return select;
}

void manual()
{
string word;

cout << endl << "INPUT WORD: " << endl;
cin >> word;

}
void file(char *roc)
{
ifstream fin("word.txt");
int x;
int count = 1;
int word;
int i = 0;


srand(time(0));

word = rand() % 20;

while (count < word)
{
fin >> x;
if (x == 0)
{
count++;
}
}


do
{
fin >> x;
roc[i++] = char(x);
} while (x);

}
int letterFill(char guess, char *secretword, char *guessword)
{
int i;
int matches = 0;

for (i = 0; i<MAX_LENGTH; i++)
{

if (secretword[i] == 0)
{
break;
}


if (guess == guessword[i])
{
return 0;
}

if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

void Unknown(char *word, char *unknown)
{
int i;
int length = strlen(word);

for (i = 0; i<length; i++)
{
unknown[i] = '*';
}

unknown[i] = 0;
}
Here is your code, I format it for clarity:

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <fstream>
#include <cstring>
#include <ctime>
#include <cstdlib>


using namespace std;
int	instructions();
void	manual();
void	file(char*);
int	letterFill(char, char*, char*);
void	Unknown(char*, char*);
const int MAX_LENGTH = 10;

int main()
{
    bool done = false;
    char word[MAX_LENGTH];
    char unknown[MAX_LENGTH];
    char letter;
    char name[MAX_LENGTH];
    int wrong_guesses = 0;
    int MAX_TRIES;
    char ans;


    while (!done)
    {
        switch (instructions())
        {
            case 1:
            {
                manual();
                break;
            }
            case 2:
            {
                file(word);
                break;
            }
        }

        cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
        cin >> MAX_TRIES;
        Unknown(word, unknown);
        cout << endl << endl << "HANGMAN";
        cout << endl << endl << "Each letter is represented by a star." << endl;
        cout << "You have " << MAX_TRIES << " tries left.";
        cout << "ENTER GUESS WHEN READY: ";
        cin >> letter;

        while (letter != 'N' && letter != 'n')
        {

            while (wrong_guesses < MAX_TRIES)
            {
                cout << unknown << endl;
                cout << "Guess a letter: " << flush;
                cin >> letter;


                if (letterFill(letter, word, unknown) == 0)
                {
                    cout << endl << "You got it wrong! You lose a guess" << endl;
                    wrong_guesses++;
                }
                else
                {
                    cout << endl << "Pfft, you got lucky" << endl;
                }

                cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;


                if (strcmp(word, unknown) == 0)
                {
                    cout << word << endl;
                    cout << "You got it!" << endl;
                    exit(0);
                }
                cout << "You've been hanged." << endl;
                cout << "The word was : " << word << endl;
            }
        }

        cout << "Try again? (y/n): " << flush;
        cin >> ans;
        if (ans == 'y' || ans == 'Y')
        done = true;
        else
        done = false;
    }
    system("pause");
}

int instructions()
{
    int select = 0;
    cout << endl << "HANGMAN" << endl << endl;
    cout << " PROGRAM MENU" << endl;
    cout << " Select option 1 or 2" << endl << endl;
    cout << " 1. INPUT WORD MANUALLY" << endl;
    cout << " 2. PLAY AGAINST THE COMPUTER" << endl;
    cout << " 3. EXIT PROGRAM BY INPUTING: N or n" << endl << endl;
    cin >> select;
    return select;
}

void manual()
{
    string word;

    cout << endl << "INPUT WORD: " << endl;
    cin >> word;

}


void file(char *roc)
{
    ifstream fin("word.txt");
    int x;
    int count = 1;
    int word;
    int i = 0;


    srand(time(0));

    word = rand() % 20;

    while (count < word)
    {
        fin >> x;
        if (x == 0)
        {
            count++;
        }
    }


    do
    {
        fin >> x;
        roc[i++] = char(x);
    } while (x);

}


int letterFill(char guess, char *secretword, char *guessword)
{
    int i;
    int matches = 0;

    for (i = 0; i<MAX_LENGTH; i++)
    {

        if (secretword[i] == 0)
        {
            break;
        }


        if (guess == guessword[i])
        {
            return 0;
        }

        if (guess == secretword[i])
        {
            guessword[i] = guess;
            matches++;
        }
    }
    return matches;
}


void Unknown(char *word, char *unknown)
{
    int i;
    int length = strlen(word);

    for (i = 0; i<length; i++)
    {
        unknown[i] = '*';
    }

    unknown[i] = 0;
}


If you will choose 1, the "word" variable in main() as below:
int main()
{
bool done = false;
char word[MAX_LENGTH];
 


will have invalid or uninitialized value since it was not filled with the input word when you call manual() since you declare a local variable word inside it as below:

1
2
3
4
5
6
7
8
void manual()
{
    string word;

    cout << endl << "INPUT WORD: " << endl;
    cin >> word;

}


That string word inside this manual() function is local to this function only, once this function exits, string word inside this function will be erased. Back in main, when you select 1 which is to input word manually, you used "word" variable many times but that variable was not initialized so that is one of the problem. Perhaps you are thinking it is initialized when you call manual() but your logic is wrong as I explained above.
Sorry but I don't know how to fix this, do you think you can update my code if it's not too much trouble
do not call manual() in case you choose 1. just put the line 5 and 6 of the function manual into the switch in case 1 like below:

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
int main()
{
    bool done = false;
    char word[MAX_LENGTH];
    char unknown[MAX_LENGTH];
    char letter;
    char name[MAX_LENGTH];
    int wrong_guesses = 0;
    int MAX_TRIES;
    char ans;


    while (!done)
    {
        switch (instructions())
        {
            case 1:
            {
                
                cout << endl << "INPUT WORD: " << endl;
                cin >> word;
                break;
            }
            case 2:
            {
                file(word);
                break;
            }
        }
My advice, do not put all codes you can think of and then test all at once. It is hard to find problems of the code. Code little by little while testing it little by little to avoid solving overwhelming problems.

THANK YOU SO MUCH, I also fixed a couple of if statements so it wouldn't display the losing and winning outputs together. I just noticed when I select option 2 to play against the computer it just doesn't do anything, any advice on that?
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int	instructions();
void	manual();
void	file(char*);
int	letterFill(char, char*, char*);
void	Unknown(char*, char*);
const int MAX_LENGTH = 10;
int main()
{
	bool done = false;
	char word[MAX_LENGTH];
	char unknown[MAX_LENGTH];
	char letter;
	char name[MAX_LENGTH];
	int wrong_guesses = 0;
	int MAX_TRIES;
	char ans;


	while (!done)
	{
		switch (instructions())
		{
		case 1:
		{
			cout << endl << "INPUT WORD:" << endl;
			cin >> word;
			break;
		}
		case 2:
		{
			file(word);
			break;
		}
		}

		cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
		cin >> MAX_TRIES;
		Unknown(word, unknown);
		cout << endl << endl << "HANGMAN";
		cout << endl << endl << "Each letter is represented by a star." << endl;
		cout << "You have " << MAX_TRIES << " tries left.";
		cout << "Enter X when ready: ";
		cin >> letter;

		while (letter != 'N' && letter != 'n')
		{

			while (wrong_guesses < MAX_TRIES)
			{


				cout << unknown << endl;
				cout << "Guess a letter: " << flush;
				cin >> letter;


				if (letterFill(letter, word, unknown) == 0)
				{
					cout << endl << "You got it wrong! You lose a guess" << endl;
					wrong_guesses++;
				}
				else
				{
					cout << endl << "Pfft, you got lucky" << endl;
				}

				cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;


				if (strcmp(word, unknown) == 0)
				{
					cout << word << endl;
					cout << "You got it!" << endl;
					cout << "The word was:  " << word << endl;
					exit(0);
				}
				if (wrong_guesses == MAX_TRIES)
				{
					cout << "You got hanged! Haha!" << endl;
					cout << "The word was:  " << word << endl;
				}

			}
		}

	}
	system("pause");
}

int instructions()
{
	int select = 0;
	cout << endl << "HANGMAN" << endl << endl;
	cout << "     PROGRAM MENU" << endl;
	cout << "  Select option 1  or  2" << endl << endl;
	cout << "      1. INPUT WORD MANUALLY" << endl;
	cout << "      2. PLAY AGAINST THE COMPUTER" << endl;
	cin >> select;
	return select;
}

void manual()
{
	string word;

	cout << endl << "INPUT WORD: " << endl;
	cin >> word;

}
void file(char *roc)
{
	ifstream fin("C:\word.txt");
	int x;
	int count = 1;
	int word;
	int i = 0;


	srand(time(0));

	word = rand() %20;

	while (count < word)
	{
		fin >> x;
		if (x == 0)

		{
			count++;
		}


	}


	do
	{
		fin >> x;
		roc[i++] = char(x);
	} while (x);

}
int letterFill(char guess, char *secretword, char *guessword)
{
	int i;
	int matches = 0;

	for (i = 0; i<MAX_LENGTH; i++)
	{

		if (secretword[i] == 0)
		{
			break;
		}


		if (guess == guessword[i])
		{
			return 0;
		}

		if (guess == secretword[i])
		{
			guessword[i] = guess;
			matches++;
		}
	}
	return matches;
}

void Unknown(char *word, char *unknown)
{
	int i;
	int length = strlen(word);

	for (i = 0; i<length; i++)
	{
		unknown[i] = '*';
	}

	unknown[i] = 0;
}


This is the updated code, the single player works fine but option 2 (player vs computer) doesn't work at all, it doesn't select the word from the txt file
I dont understand the logic inside file() function. What do you want it to do? Explain each step you want to do in there so i can help.
Topic archived. No new replies allowed.