Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help compiling some errors that I am having on my Hangman code

WinterhawkWinterhawk Posts: 4Member
[q][b]I am getting the same error about seventeen different times in the same file(Game.cpp). I know that it has something to do with arrays and pointers but, I am not sure how to fix the problem. Here is the errors that I am getting when I try to compile my code. The errors are coming from my Game file. 1>Game.cpp
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(32) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(38) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(41) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(57) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(57) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(60) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(60) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(63) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(63) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(109) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(113) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(117) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(117) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(131) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(138) : error C2109: subscript requires array or pointer type
1>c:documents and settingskevin oxendine.kevinmy documentsvisual studio 2008projectsoxendinetermprojectkoxendineweek3game.cpp(138) : error C2109: subscript requires array or pointer type

And here is my code.[/q][/b]

Game.cpp
[code]
#include "Game.h"
#include

using namespace std;

ostream& operator<< (ostream& osObject, const Game& theGame)
{
osObject << "Player's name:" << theGame.plyrName; //Display player's name
return osObject;
}
istream& operator>> (istream& isObject, Game& theGame)
{
isObject >> theGame.plyrName; //Gets player's name
return isObject;
}
Game::Game()
{
letAmount = 0; // letAmount is amount of letters that are in our hangman word
guessAmount = 0; // guessAmount is the amount of guesses that the player as made wrong
correct = 0; // correct is the amount of correct guesses the player as made
score = 0; // score is what holds the total points scored by the player
letValue = 0; // letValue is what holds the value of the current letter guessed and how
// it effects the players score
startLet = 97; // startLet is just a "char" used to signify what ASCII code for the
// first letter in the alphabet is (lowercase) 97 = 'a' && 122 = 'z'
gameOver = false; // gameOver is our boolean which gets returned to the method "isGameOver" to
// see if our game is over or not. = ]


for(int abc = 0; abc < 27; abc++) // Loop to store lowercased alphabet
{ //alpha[] just holds the alphabet (size of array is 27)
alpha[abc] = startLet; // this asigns the first subscript of alpha to a lowercased 'a'

startLet++; // this increases startLet by one which will make it a lowercase 'b'
}

for(int letG = 0; letG < 27; letG++)
letGuessed[letG] = 32; // letGuessed holds all the letters that the player has guessed.

for(int hmlt = 0; hmlt < 7; hmlt++)
hmLetters[hmlt] = 32; // hmLetters holds all the letters that the player as guessed correctly
// that were part of the word


hmWord = new char[letAmount];
}
Game::Game(const Game &game)
{
letAmount = game.letAmount;
guessAmount = game.guessAmount;
correct = game.correct;
score = game.score;
letValue = game.letValue;
gameOver = game.gameOver;

for(int abc = 0; abc < 27; abc++)
alpha[abc] = game.alpha[abc];

for(int letG = 0; letG < 27; letG++)
letGuessed[letG] = game.letGuessed[letG];

for(int hmlt = 0; hmlt < letAmount; hmlt++)
hmLetters[hmlt] = game.hmLetters[hmlt];

for(int hm = 0; hm < letAmount; hm++)
hmWord[hm] = game.hmWord[hm];
}

void Game::play()
{
bool guessLet(char [], char &, int, int, int, bool &);
{
if(correct == letAmount) // checks to see if the player has made the correct
{ // amount of guesses and if the player as, then display
cout << endl; // a "Winner" message and end game!
cout << " Congratulations! You Saved Hangman! You Win!
";
cout << endl;
gameOver = true;
return ;
}

if(guessAmount == 9) // checks to see if the player has made to many incorrect
{ // guesses, and if the player as made 9 incorrect guesses then
cout << endl; // display a "Lost" message and end game!
cout << " Game Over! Better Luck Next Time!
";
cout << endl;
gameOver = true;
return ;
}

// Ask the user to guess a letter = ]
cout << endl << " Guess a letter that you think is in the word? ";
cin >> guess; // asigns "guess" to the letter the player guessed

guess = tolower(guess); // "tolower()" takes the value of that variable
// and makes it lower case.

return ; // returns false
}
void checkGuess(char, char [], char [], char [], char [], int &, int &, int);
{
// If guess is correct:
for(int i = 0; i < letAmount; i++)
{
if(guess == hmWord[i])
{
correct++;

hmLetters[i] = hmWord[i];

for(int s = 0; s < 27; s++)
{
if(guess == alpha[s])
{
letValue = s + 1;
score += letValue;
letGuessed[s] = alpha[s];
} // eof if(guess == alpha[s])
} // eof for(int s = 0; s < 27; s++)
} // eof if(guess == hmWord[i])
} // eof for(int i = 0; i < letAmount; i++)

// If the word was incorrect:

if( (guess != hmWord[0]) && (guess != hmWord[1]) && (guess != hmWord[2]) && (guess != hmWord[3]) && (guess != hmWord[4]) && (guess != hmWord[5]) && (guess != hmWord[6]) )
{
guessAmount++;

for(int s = 0; s < 27; s++)
{
if(guess == alpha[s])
{
letValue = s + 1;
score -= letValue;

if(score < 0) score = 0;

letGuessed[s] = alpha[s];

} // eof if(guess == alpha[s])
} // eof for(int s = 0; s < 27; s++)
} // eof if( (guess != hmWord[0]) && (guess != hmWord[1]) && (guess != hmWord[2]) && (guess != hmWord[3]) && (guess != hmWord[4]) && (guess != hmWord[5]) && (guess != hmWord[6]) )
}

}


[/code]

[q][b]I would like to say thinks ahead of time to those that may give me any tips or suggestion.[/q][/b]

Comments

  • LundinLundin Posts: 3,711Member
    You didn't post the h-file, so it isn't possible to compile the code. So please post it and I will take a look at the code.
  • WinterhawkWinterhawk Posts: 4Member
    : You didn't post the h-file, so it isn't possible to compile the
    : code. So please post it and I will take a look at the code.

    I am at work right now just check back later on about 8:00pm est time and I will post the h-file than. Thank you for your help.
  • WinterhawkWinterhawk Posts: 4Member
    : You didn't post the h-file, so it isn't possible to compile the
    : code. So please post it and I will take a look at the code.

    [q]Here is that h-file.[/q]

    Game.h
    [code]
    #ifndef Game_h
    #define Game_h
    #include
    using namespace std;

    struct BODY // this is my structure that holds all the pieces of Hangman for when
    { // he get's drawed to the screen.

    char head, head2, headCLT, headMT, headCRT, headCLB, headMB, headCRB,
    neck,
    shoulders, shouldersL, shouldersM, shouldersR,
    arm1, arm2,
    body,
    hips, hipsL, hipsM, hipsR,
    leg1, leg2;
    }; // eof structure BODY

    class Game
    {
    //Overload the stream insertion and extraction operators
    friend ostream& operator<< (ostream&, const Game &);
    friend istream& operator>> (istream&, Game &);

    public:

    virtual void play();
    Game(); // constructor
    Game(const Game &game); // copy constructor

    protected:

    string skipWord, plyrName;
    char *hmWord, guess, startLet;
    int letAmount, guessAmount, correct,
    score, letValue, alpha, letGuessed, hmLetters;
    bool gameOver;

    };
    #endif


    [/code]


  • LundinLundin Posts: 3,711Member
    alpha, letGuessed, hmLetters

    These aren't declared as arrays. Im surprised your compiler didn't point this out clearly, what compiler are you using?

    Also, the golden rule of object oriented programming in C++ is that [italic]if a class needs a copy constructor, an overloaded assignment operator or a destructor, it needs all three of them[/italic].

    If you allocate the variables mentioned above dynamically, then you need a copy constructor. Otherwise, you don't need one, statically allocated arrays are always copied automatically, just as plain variables are.
  • WinterhawkWinterhawk Posts: 4Member
    : alpha, letGuessed, hmLetters
    :
    : These aren't declared as arrays. Im surprised your compiler didn't
    : point this out clearly, what compiler are you using?
    :
    : Also, the golden rule of object oriented programming in C++ is that
    : [italic]if a class needs a copy constructor, an overloaded
    : assignment operator or a destructor, it needs all three of
    : them[/italic].
    :
    : If you allocate the variables mentioned above dynamically, then you
    : need a copy constructor. Otherwise, you don't need one, statically
    : allocated arrays are always copied automatically, just as plain
    : variables are.

    [q]So if I declare this three as arrays would they or could they look something like this. int alpha[89] and should this be equal to something and the same way with the others ones.

    Also how would I fix this into my code because, I feel that once I change this around I will have more errors on my hands.

    If you are talking about what software I am useing when you say my compiler it is Microsoft Visual Studio 2008.[/q]


  • LundinLundin Posts: 3,711Member
    : So if I declare this three as arrays would they or could they
    : look something like this. int alpha[89]


    Yes, unless there is a need to adjust the size in runtime, then you need dynamic allocation.


    : and should this be equal to something and the same way with the others ones.

    I don't understand your English here.


    : Also how would I fix this into my code because, I feel that once I
    : change this around I will have more errors on my hands.

    If you go for static allocation, you just need to do "int alpha[89]" for all three of them and the program should compile.


    : If you are talking about what software I am useing when you say my
    : compiler it is Microsoft Visual Studio 2008.[/q]

    Ok, that compiler can find said errors just fine.
Sign In or Register to comment.