Thread: Class not working!!! HELP

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Class not working!!! HELP

    My player class is not updating the player gold counter when I increment it in the function. I have posted the code for the three files... Any idea why it is not working, I have no errors or warnings from my compiler...

    code for class header file

    Code:
    #ifndef HAUNT_H
    
    #define HAUNT_H
    
    /******** file librarys for game **********************************************/
    
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <windows.h>	// allow windows functions
    
    using namespace std;	// use entire namespace
    /******** function prototypes *************************************************/
    
    /* SET UP PLAY */
    void FullScreen ( void );										// set game to full screen
    void DrawColorString(string szText, int X, int Y, WORD color);	// text color
    void gotoxy( int x, int y ); 									// game co-ordinates
    void intro ( void );											// NightBreed logo
    void title ( void );											// title screen
    void load ( void );												// load game
    void gameover ( void );											// game over screen
    void tb( void );												// player status
    void bb( void );												// player info pannel
    
    // TODO: add game level names and places here
    
    void area1 ( void );
    
    /******* player class *********************************************************/
    
    class Player
    {
    public:
    	   Player();	// constructor
    	   ~Player();	// deconstructor
    	   
    	   int health;		// hp
    	   int magic;		// mp
    	   string pname;	// name of player
    	   int gold;		// gold to buy goods
    	   
    private:
    		bool pdead;		// is player defeated ( at start of play )
    		bool palive;	// is player active ( at start of play )
    };
    
    /******** enemy class *********************************************************/
    
    class Enemy
    {
    public:
    	   Enemy();		// constructor
    	   ~Enemy();	// deconstructor
    	   
    	   string ename;	// enemy name
    	   int ehealth;		// enemy health;
    	   int edamage;		// enemy damage to player
    	   
    private:
    		bool edead;		// is enemy defeated
    		bool ealive;	// is enemy active
    };
    
    #endif /* HAUNT_H */
    code for class implentation file

    Code:
    #include "haunt.h"
    
    /***************** PLAYER *****************************************************/
    
    Player::Player()
    {
         health = 100;
    	 gold = 5;
    	 magic = 0;
    	 pname;
     				
         palive = true;	// new game
         pdead = false;	// turns TRUE when player defeated
    }
    
    Player::~Player()
    {
    }
    
    /****************** ENEMY *****************************************************/
    
    Enemy::Enemy()
    {
        ehealth;
    	edamage;
     	ename;
     			  
        ealive = true;	// on enemy encounter
      	edead = false;	// turns TRUE when enemy defeated
    }
    
    Enemy::~Enemy()
    {
    }
    
    void gotoxy( int x, int y ) 
    { 
        COORD c; 
    
    	c.X = x - 1 ; 
     	c.Y = y - 1 ; 
     	
     	// TO REMEMBER: 1ST NUMBER SPACES IN :: 2ND NUMBER SPACES DOWN
    
    	SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), c ) ; 
    } 
    
    
    // string colour graphic function
    void DrawColorString(string szText, int X, int Y, WORD color)
    {	
    	HANDLE OutputH;									
    	COORD position = {X, Y};							
    
    	OutputH = GetStdHandle(STD_OUTPUT_HANDLE);		
    
    	SetConsoleTextAttribute(OutputH, color);		
    													
    	SetConsoleCursorPosition(OutputH, position);	
    
    	cout << szText;									
    }
    
    // full screen mode ( optional )
    void FullScreen ( void )
    {
     	 keybd_event(VK_MENU, 0x38, 0, 0);
    	 keybd_event(VK_RETURN, 0x1c, 0, 0);
    	 keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0);
    	 keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);
    }
    
    void tb ( void )
    {
     	 Player play;
     	 
     	 system("cls");
     	 
     	 DrawColorString("", 0, 5, FOREGROUND_GREEN);
     	 gotoxy(2,1);cout << "PLAYER:";
     	 DrawColorString("", 0, 5, FOREGROUND_RED | FOREGROUND_GREEN);
     	 gotoxy(11,1);cout << play.pname;
     	 DrawColorString("", 0, 5, FOREGROUND_GREEN);
     	 gotoxy(2,3);cout << "HEALTH:";
    	 DrawColorString("", 0, 5, FOREGROUND_RED | FOREGROUND_GREEN);
    	 gotoxy(11,3);cout << play.health;
    	 DrawColorString("", 0, 5, FOREGROUND_GREEN);
    	 gotoxy(45,1);cout << "HP:";
    	 DrawColorString("", 0, 5, FOREGROUND_RED | FOREGROUND_GREEN);
    	 gotoxy(50,1);cout << play.magic;
    	 DrawColorString("", 0, 5, FOREGROUND_GREEN);
    	 gotoxy(45,3);cout << "GOLD:";
    	 DrawColorString("", 0, 5, FOREGROUND_RED | FOREGROUND_GREEN);
    	 gotoxy(58,3);cout << play.gold;
    	 DrawColorString("", 0, 5, FOREGROUND_BLUE);
    }
    
    // create message box at bottom of screen
    void bb ( void )
    {
    	 gotoxy(2,42);cout << "**************************************************************************";
    	 gotoxy(2,43);cout << "*";
    	 gotoxy(75,43);cout << "*";
    	 gotoxy(2,44);cout << "*";
    	 gotoxy(75,44);cout << "*";
    	 gotoxy(2,45);cout << "*";
    	 gotoxy(75,45);cout << "*";
    	 gotoxy(2,46);cout << "*";
    	 gotoxy(75,46);cout << "*";
    	 gotoxy(2,47);cout << "*";
    	 gotoxy(75,47);cout << "*";
    	 gotoxy(2,48);cout << "**************************************************************************";
    }
    code for main

    Code:
    #include "haunt.h"
    
    int main(int argc, char *argv[])
    {
     	intro();
     	
    }
    
    void intro ( void )
    {
     	 FullScreen();
     	 
     	 DrawColorString("", 0, 5, FOREGROUND_RED);
     	 
     	 gotoxy(15,20);cout << "NightBreed Software Presents";
     	 
     	 Sleep(3000);
     	 
     	 area1();
    }
    
    void area1 ( void )
    {
     	 Player play;
     	 
     	 tb();
     	 bb();
     	 
     	 play.gold = play.gold + 10;
     	 
     	 cout << "GOLD IS NOW: " << play.gold;
     	 
     	 getch();
    }
    code i cannot get to work is play.gold = play.gold + 10

    Any ideas???

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You arent passing play to the print-function. And when you create a new player in the print-function you will just use the default values. Read up on passing data betwean functions.

    What you are doing now is basicly this
    Code:
    #include <iostream>
    
    void print()
    {
      int a = 10;
      std::cout << a << std::endl;
    }
    void somefunc()
    {
      int a = 20;
      print();
    }
    
    int main()
    {
      somefunc();
    }
    With your logic you would expect the output to be 20 but it will always be 10. To actuall print the actual variable you would need to do this:
    Code:
    #include <iostream>
    
     // telling the function that it expects an int as argument
    void print(int a) 
    {
      std::cout << a << std::endl;
    }
    void somefunc()
    {
      int a = 20;
      print(a);  // sending a, but we could also send a varaible with any name
    }
    
    int main()
    {
      somefunc();
    }
    Last edited by Shakti; 02-16-2006 at 06:51 AM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  2. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. working with a class function
    By Noobie in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2003, 07:07 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM