noob coding on minor hangman game issues?

I feel it is so close, but I am not able to get the game to work right. It prints out both winning and losing lines whether or not the player guesses a correct letter. I tried this after watching a tutorial, but I'm not sure where I went wrong or if I should just start over. Perhaps I was too ambitious. To be clear, this is for school but I am allowed outside help, just not stealing others work, or letting someone else code for me. I want to understand my mistakes, not just gloss over them. anything would be appreciated. TOTAL NOOB






// This was added so it would run on my compiler. I am
// not sure you need it.... Jim Shain
//#include "stdafx.h"



#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;

bool running = 1;

int playerLives = 7; // Our hangman has 7 stages, so the player starts with 7 lives
char knownLetters[50]; // The word could be of any length
string word = "";
//string word = wordList[wordID];
string GenerateWord();
void PrintLetters();
void PrintMan();
void PlayerLose();
void PlayerWin();


// I put PrintMan and the others here. Jim
void PrintMan() {

switch(playerLives) {
case 7:
cout << "\n\n\n\n\n";
break;
case 6:
cout << "\n\n\n\n______";
break;
case 5:
cout << "\n |\n |\n |\n |\n____|_";
break;
case 4:
cout << " ___\n | \\|\n |\n |\n |\n____|_";
break;
case 3:
cout << " ___\n | \\|\n O |\n |\n |\n____|_";
break;
case 2:
cout << " ___\n | \\|\n O |\n | |\n |\n____|_";
break;
case 1:
cout << " ___\n | \\|\n O |\n/|\\ |\n |\n____|_";
break;
case 0:
cout << " ___\n | \\|\n O |\n/|\\ |\n/ \\ |\n____|_";
break;
}


cout << "\n"; // For formatting purposes

return;
}

void PrintLetters() {
for(int pos = 0; pos < word.length(); pos++) {
if(knownLetters[pos] == word[pos]) cout << word[pos];
else cout << "_";
}


return;
}


// Jim
string GenerateWord() {
srand(time(NULL)); // Use the system time as a seed
int wordID = rand() % 11; // Replace 11 with the amount of words you have + 1

string word = "";
string wordList[10] = { "lannister", "danarys", "dothraki", "stark", "baratheon", "snow", "arryn", "greyjoy", "tully", "tyrell" };
return word;
}



void PlayerWin() {
cout << "yay, you guessed the word correctly! winter is coming";
cout << "Press ENTER to quit\n";
cin.get();
running = 0;

return;
}

void PlayerLose() {
cout << "You lose! may the seven gods forgive you. The word was: " << word << "\n";
cout << "Press ENTER to quit\n";
cin.get();
running = 0;

return;
}



int main() {
word = GenerateWord(); // Pick a random word
char userGuess;

while(running) {

cout << "Guess a letter, hint the word is a house in game of thrones: ";
cin >> userGuess;
bool correct = 0;


//original
//for(int pos = 0; pos < word.length; pos++) {
// The length of a string is a function so you need parens after it. - Jim
for(int pos = 0; pos < word.length(); pos++) {
if(userGuess == word[pos]) {
knownLetters[pos] = userGuess; // If the letter is in the word, mark it as guessed
correct = 1;
}
}

// Jim
int lives;


if(correct == 0) lives--; // If the letter was wrong, subtract a life
correct = 0;

PrintMan();

PrintLetters();

if(lives <= 0) PlayerLose();

// Jim
string knowLetters;



if(knowLetters == word) PlayerWin();

}




cout << "\n";


// Jim main must return a value
//original
//return;
return(0);
}

Please use code tags.

Your code with code tags and indentation:

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
#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;

bool running = 1;

int playerLives = 7; // Our hangman has 7 stages, so the player starts with 7 lives
char knownLetters[50]; // The word could be of any length
string word = "";
//string word = wordList[wordID];
string GenerateWord();
void PrintLetters();
void PrintMan();
void PlayerLose();
void PlayerWin();


// I put PrintMan and the others here. Jim
void PrintMan() {

    switch(playerLives) {
    case 7:
    cout << "\n\n\n\n\n";
    break;
    case 6:
    cout << "\n\n\n\n______";
    break;
    case 5:
    cout << "\n |\n |\n |\n |\n____|_";
    break;
    case 4:
    cout << " ___\n | \\|\n |\n |\n |\n____|_";
    break;
    case 3:
    cout << " ___\n | \\|\n O |\n |\n |\n____|_";
    break;
    case 2:
    cout << " ___\n | \\|\n O |\n | |\n |\n____|_";
    break;
    case 1:
    cout << " ___\n | \\|\n O |\n/|\\ |\n |\n____|_";
    break;
    case 0:
    cout << " ___\n | \\|\n O |\n/|\\ |\n/ \\ |\n____|_";
    break;
    }


    cout << "\n"; // For formatting purposes

    return;
}

void PrintLetters() {
    for(int pos = 0; pos < word.length(); pos++) {
        if(knownLetters[pos] == word[pos]) cout << word[pos];
        else cout << "_";
    }


    return;
}


// Jim
string GenerateWord() {
    srand(time(NULL)); // Use the system time as a seed
    int wordID = rand() % 11; // Replace 11 with the amount of words you have + 1

    string word = "";
    string wordList[10] = { "lannister", "danarys", "dothraki", "stark", "baratheon", "snow", "arryn", "greyjoy", "tully", "tyrell" };
    return word;
}



void PlayerWin() {
    cout << "yay, you guessed the word correctly! winter is coming";
    cout << "Press ENTER to quit\n";
    cin.get();
    running = 0;

    return;
}

void PlayerLose() {
    cout << "You lose! may the seven gods forgive you. The word was: " << word << "\n";
    cout << "Press ENTER to quit\n";
    cin.get();
    running = 0;

    return;
}



int main() {
    word = GenerateWord(); // Pick a random word
    char userGuess;

    while(running) {

        cout << "Guess a letter, hint the word is a house in game of thrones: ";
        cin >> userGuess;
        bool correct = 0;


        //original
        //for(int pos = 0; pos < word.length; pos++) {
        // The length of a string is a function so you need parens after it. - Jim
        for(int pos = 0; pos < word.length(); pos++) {
            if(userGuess == word[pos]) {
                knownLetters[pos] = userGuess; // If the letter is in the word, mark it as guessed
                correct = 1;
            }
        }

        // Jim
        int lives;


        if(correct == 0) lives--; // If the letter was wrong, subtract a life
        correct = 0;

        PrintMan();

        PrintLetters();

        if(lives <= 0) PlayerLose();

        // Jim
        string knowLetters;



        if(knowLetters == word) PlayerWin();

    }




    cout << "\n";


    // Jim main must return a value
    //original
    //return;
    return(0);
}



Problems:

- On line 122 you create a variable named "lives" but you do not initialize it, therefore its value is garbage/worthless. You probably meant to use the global "playerLives" instead of making an entirely new variable.

- Similar problem on line 135. You create another string named "knowLetters" but never assign it to anything, so it's an empty string. You probably meant to use the global "knownLetters" instead of a new variable.

- On line 73 you create a "word" variable, but you never assign it to anything. As a result, your GenerateWord function returns an empty string every time. wordList and wordID never get used.


After those fixes it seems to work for me.
got it! thank you so much, I know this is cheesy, but it was mostly proofreading, thank you for pointing out my errors.

I got the random word generator to work as well, with

string word = wordList[wordID];;

so cool. time to clean it all up, make a bigger and badder word list, and put in a lot of comments. my first game ever! (well, one built by me, and understood!)

:)
Topic archived. No new replies allowed.