Help with C++ Word Guess Game

So I've been trying to get this word guess game working, but the word so far is not showing.

For instance, if the word input by Player 1 is "bell" and Player 2 guesses b, the word will not show b***, it will only output "Your word so far is: ."

(Keep in mind I know nothing of functions, vectors, arrays, or pointers)

Here's my code, I appreciate any help!
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
string secret;
char guess;
const int wrong_max = 10;
int wrong = 0;
string soFar (secret.size(), '*');
string used = " ";

cout << "Player 1, what's the secret word for Player 2 to guess?" << endl;
cin >> secret;
cout << "Player 2, you have 10 guesses so make them count!" << endl;

while (wrong < wrong_max && soFar != secret)
{
cout << "You have " << (wrong_max - wrong) << " this many guesses left." << endl;
cout << "You've guessed: " << used << "." << endl;
cout << "Your word so far is: " << soFar << "." << endl;
cout << "Please guess a letter." << endl;
cin >> guess;

used += guess;

if (secret.find(guess) != string::npos)
{
cout << "Yep " << guess << " is a letter in the word." << endl;

for (int i = 0; i < secret.length(); ++i)
{
if (tolower(secret[i]) == tolower(guess))
{
soFar[i] = guess;
}
}
}
else
{
cout << "Nope! Try again!" << endl;
++wrong;
}
}

if (wrong == wrong_max)
{
cout << "Ooops, better luck next time!" << endl;
}
else
{
cout << "Hey! You got it! Woo!" << endl;
}

return 0;
}
Last edited on
Not sure how you're running this. The code you posted has numerous compile errors.

You're missing the following:
1
2
3
#include <string>
#include <iostream>
using namespace std;


Line 7: secretWord is undefined.
Oops, i didn't copy and paste, i just wrote it out from another program i was running it on.

Should be fixed now.

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
#include <iostream>
#include <string>
#include<cctype>
#include<stdio.h>

using namespace std;

int main()
{
string secret;
char guess;
const int wrong_max = 10;
int wrong = 0;
string soFar (secret.size(), '*');
string used = " ";

cout << "Player 1, what's the secret word for Player 2 to guess?" << endl;
cin >> secret;
cout << "Player 2, you have 10 guesses so make them count!" << endl;

while (wrong < wrong_max && soFar != secret)
{
cout << "You have " << (wrong_max - wrong) << " this many guesses left." << endl;
cout << "You've guessed: " << used << "." << endl;
cout << "Your word so far is: " << soFar << "." << endl;
cout << "Please guess a letter." << endl;
cin >> guess;

used += guess;

if (secret.find(guess) != string::npos)
{
cout << "Yep " << guess << " is a letter in the word." << endl;

for (int i = 0; i < secret.length(); ++i)
{
if (tolower(secret[i]) == tolower(guess))
{
soFar[i] = guess;
}
}
}
else
{
cout << "Nope! Try again!" << endl;
++wrong;
}
}

if (wrong == wrong_max)
{
cout << "Ooops, better luck next time!" << endl;
}
else
{
cout << "Hey! You got it! Woo!" << endl;
}

return 0;
}
The problem is you're initializing soFar at line 14. At that point, the secret word has not been entered, so the size() of secret is 0, therefor soFar is initialized as an empty string, not with some number of asterisks.

Change your code as follows and you should be fine.
14
15
16
17
18
19
    string soFar;
    string used = " ";

    cout << "Player 1, what's the secret word for Player 2 to guess?" << endl;
    cin >> secret;
    soFar = string (secret.size(), '*');  // Now we can initialize soFar  

Topic archived. No new replies allowed.