Hangman game

I made a hangman game for my homework assignment, and naturally it doesn't work right. Can somebody help in me in identifying the problem ? The retry function does not work properly. And the code doesn't recognize a correct input. On every input it says that its wrong.

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

using namespace std;

const int Lives = 6;
int LivesLost;
string SecretWord;
string guessed;
string used;

bool match(char letter, string word);
char askGuess(string usedLettersStr);
bool Retry();

char String[][15]= {"apple", "banana", "cat", "dog", "elephant", "fish", "giraffe", "hippopotamus", "ice cream","jelly", "kangaroo", "lion", "monkey", "noodles", "octopus", "parrot", "queen", "rabbit", "sun", "tree", "umbrella", "van", "window", "xray", "yellow", "zebra"};

char Head(){
    cout << " o" << endl;
    return 0;
}
char RightArm(){
    cout << "-";
    return 0;
}
char Body(){
    cout << "X";
    return 0;
}
char LeftArm(){
    cout << "-" << endl;
    return 0;
}
char RightLeg(){
    cout << "/";
    return 0;
}
char LeftLeg(){
    cout << " \\" << endl;
    return 0;
}


void Hangman(){
    if (LivesLost > 0)
        Head();
    if (LivesLost > 1)
        RightArm();
    if (LivesLost > 2)
        Body();
    if (LivesLost > 3)
        LeftArm();
    if (LivesLost > 4)
        RightLeg();
    if (LivesLost > 5){
        LeftLeg();
        cout << "YOU LOST!";
    }
}

int main()
{
    do{
        srand(time(0));
        int randomword;
        srand(time(NULL));
        randomword = (rand() % 25) + 1;
        SecretWord = String[randomword];

        guessed = string(SecretWord.size(), '-');
        used = "";

        while((LivesLost < Lives) && (guessed != SecretWord)){
            cout << endl << endl << "You have " << (Lives - LivesLost) << " incorrect guesses left.\n";
            cout << endl << "You've used the following letters:" << endl << used << endl;
            cout << endl << "So far, the word is:" << endl << guessed << endl;

            used += askGuess(used);
        }

        if (LivesLost = Lives)
            cout << endl << "You Lost the game. Try again." << endl;

        cout << "The word was " << SecretWord << endl;
    }while (Retry());
    return 0;
}

inline bool match(char letter, string word)
{
       return ( word.find(letter) != string::npos );
}

char askGuess(string usedLettersStr)
{
    char guess;
    cout << endl << endl << "Enter your guess: ";
    cin >> guess;
    guess = toupper(guess);
    while (match(guess, used))
    {
        cout << endl << "You've already used " << guess << endl;
        cout << "Enter your guess: ";
        cin >> guess;
        guess = toupper(guess);
    }


    if (match(guess, SecretWord))
    {
        cout << guess << " is in the word!!" << endl;

        for (int i = 0; i < SecretWord.length(); ++i)
            if (SecretWord[i] == guess)
                guessed[i] = guess;
    }
    else
    {
        cout << "Sorry, " << guess << " isn't in the word." << endl;
        LivesLost++;
        Hangman();
    }
}

bool Retry()
{
     char again;
     cout << endl << endl << "Would you like to play again? <y/n>: ";
     cin >> again;

     cin.clear();
     cin.ignore();

     again = toupper(again);

     system("cls");

     return (again == 'Y');
}
Last edited on
Topic archived. No new replies allowed.