C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help compiling some errors that I am having on my Hangman code Posted by Winterhawk on 7 Dec 2008 at 3:32 PM
[q]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 settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(32) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(38) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(41) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(57) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(57) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(60) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(60) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(63) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(63) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(109) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(113) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(117) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(117) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(131) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(138) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\kevin oxendine.kevin\my documents\visual studio 2008\projects\oxendinetermproject\koxendineweek3\game.cpp(138) : error C2109: subscript requires array or pointer type

And here is my code.[/q]


Game.cpp
#include "Game.h"
#include <iostream>

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 << "\t\t\tCongratulations! You Saved Hangman! You Win!\n";
        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 << "\t\t\tGame Over! Better Luck Next Time!\n";
        cout << endl;
        gameOver = true;
            return ;
    }

    // Ask the user to guess a letter = ]
    cout << endl << "\t\tGuess 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]) )
    }

}




[q]I would like to say thinks ahead of time to those that may give me any tips or suggestion.[/q]
Report
Re: Help compiling some errors that I am having on my Hangman code Posted by Lundin on 8 Dec 2008 at 2:45 AM
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.
Report
Re: Help compiling some errors that I am having on my Hangman code Posted by Winterhawk on 8 Dec 2008 at 9:48 AM
: 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.
Report
Re: Help compiling some errors that I am having on my Hangman code Posted by Winterhawk on 8 Dec 2008 at 3:54 PM
: 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
#ifndef Game_h
#define Game_h
#include <string> 
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





Report
Re: Help compiling some errors that I am having on my Hangman code Posted by Lundin on 9 Dec 2008 at 12:54 AM
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 if a class needs a copy constructor, an overloaded assignment operator or a destructor, it needs all three of them.

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.
Report
Re: Help compiling some errors that I am having on my Hangman code Posted by Winterhawk on 9 Dec 2008 at 3:38 PM
: 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
: if a class needs a copy constructor, an overloaded
: assignment operator or a destructor, it needs all three of
: them
.
:
: 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]


Report
Re: Help compiling some errors that I am having on my Hangman code Posted by Lundin on 10 Dec 2008 at 6:50 AM
: 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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.