Andrew Wilson
Andrew Wilson

Reputation: 23

Difficulty comparing two arrays

#include<iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
char wordOne[5][9] = { {"elephant"},
                       {"imperial"},
                       {"absolute"},
                       {"absinthe"},
                       {"computer"} };

char hangman[9] = {"********"};

char guess;

int r;
srand( time(0));
r = rand()%5;
wordOne[r];

cout << "Guess the secret eight letter word before you run out of guesses! Go: " << endl;

for (int x = 0; x < 8; ++x)

cout << hangman[x] << endl;
cin >> guess;

while (hangman[0] == '*' || hangman[1] == '*' || hangman[2] == '*' || hangman[3] == '*' || hangman[4] == '*' || hangman[5] == '*' || hangman[6] == '*' || hangman[7] == '*')

{
cout << "Guess the secret eight letter word before you run out of guesses! Go: ";
for(int x = 0; x < 8; ++x)
        cout << hangman[x];
        cout << endl;
        cin >> guess;

    for(int x = 0; x < 8; ++x)
    {
        if (wordOne[hangman[x]][x] == guess)
            {
                hangman[x] = guess;
            }
    }

for(int x = 0; x < 8; ++x)
    cout << hangman[x] << endl;
}

system("PAUSE");
return 0;
}

For a project, we were asked to create a one dimensional array that only displays asterisks. Then, using a two dimensional array, store 5 different 8 letter words. The program is supposed to select one at random, and then the user would enter random letters trying to guess the word. It's essentially replicating hangman. As letters are guessed, the asterisk is supposed to be replaced by the correct letter. For instance, if the word was elephant and an e was guessed first, the program would then display e*** and so on. I have gotten the program to compile but I'm not able to figure out how to change the code so that asterisks are replaced and the program runs properly. Any feedback would be much appreciated.

Upvotes: 0

Views: 586

Answers (2)

Jonny Henly
Jonny Henly

Reputation: 4213

wordOne[hangman[x]][x] let's say x = 1 this would equate in code to wordOne['*'][1]. Shouldn't it be wordOne[r][x] == guess? I added an int to keep track of the amount of guesses and added a check in the while loop to see if the user has guessed the maximum number of times.

#include<iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    char wordOne[5][9] = { {"elephant"},
    {"imperial"},
    {"absolute"},
    {"absinthe"},
    {"computer"} };

    char hangman[9] = {"********"};

    char guess;

    int r;
    srand( time(0));
    r = rand()%5;
    wordOne[r];
    int numGuesses = 10;

    cout << "Guess the secret eight letter word before you run out of guesses! Go: " << endl;

    for (int x = 0; x < 8; ++x)
        {
        cout << hangman[x] << endl;
    }
    while (numGuesses > 0 && (hangman[0] == '*' || hangman[1] == '*' || hangman[2] == '*' || hangman[3] == '*' || hangman[4] == '*' || hangman[5] == '*' || hangman[6] == '*' || hangman[7] == '*'))

    {
        cout << "Guess the secret eight letter word before you run out of guesses! Go: ";


        for(int x = 0; x < 8; ++x)
        {
            cout << hangman[x];
        }

        cout << endl;

        cin >> guess;
        --numGuesses;

        for(int x = 0; x < 8; ++x)
         {
            if (wordOne[r][x] == guess)
            {
                hangman[x] = guess;
            }
        }

        for(int x = 0; x < 8; ++x)
        {
            cout << hangman[x] << endl;
        }
    }

    system("PAUSE");
    return 0;
}

Upvotes: 3

McLovin
McLovin

Reputation: 3674

Change wordOne[hangman[x]][x] to wordOne[r][x]. Also, I'd recommend not printing endl in for loops, as it prints each character on a different line.

Upvotes: 2

Related Questions