Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


C++ problem problem!

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
ShurikeN
Advanced Cheater
Reputation: 0

Joined: 09 Jan 2008
Posts: 84

PostPosted: Thu Jan 31, 2008 8:39 am    Post subject: C++ problem problem! Reply with quote

What's wrong with my code, when i enter my health and then call the function "stats()" to display the value of health but instead it displays wrong value -something like 208809675..-

Code:
#include <iostream>
#include <conio.h>
using namespace std;
void stats();

class PLAYER
    {
          private:
                  int health;
          public:
                 void sethealth(int);
                 int gethealth();
    };
                 
void PLAYER::sethealth(int amount)
    {
         health = amount;
    }
     
int PLAYER::gethealth()
     {
         return health;
     }

void stats()
     {
         PLAYER player;
         cout << "Your health is: " << player.gethealth();
     }

int main()
    {
        PLAYER player;
        int gethp;
        cout << "Enter your health: ";
        cin >> gethp;
        player.sethealth(gethp);
        stats();
        _getch();
    }



edit:

aw! i just noticed even if i don't enter a value for health it still displays the same number 208809675..

_________________
Code:
XXXXXX      XXXXXX
   XXXXX  XXXXX
     XXXXXXXX
    D I R E C T
     XXXXXXXX
   XXXXX  XXXXX
XXXXXX      XXXXXX
      GameDev


Last edited by ShurikeN on Thu Jan 31, 2008 8:46 am; edited 1 time in total
Back to top
View user's profile Send private message
ThisIsImpossible
Cheater
Reputation: 0

Joined: 06 Dec 2007
Posts: 28

PostPosted: Thu Jan 31, 2008 8:45 am    Post subject: ... Reply with quote

Razz Click in error debug or something and it takes you automaticly for wrong code Razz Then u see.
Back to top
View user's profile Send private message
the_undead
Expert Cheater
Reputation: 1

Joined: 12 Nov 2006
Posts: 235
Location: Johannesburg, South Africa

PostPosted: Thu Jan 31, 2008 8:46 am    Post subject: Reply with quote

Because for each case youre calling PLAYER player, as a result each function has its own, independent version of PLAYER.

make it global.
just call it once somewhere near the top.

_________________
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
ShurikeN
Advanced Cheater
Reputation: 0

Joined: 09 Jan 2008
Posts: 84

PostPosted: Thu Jan 31, 2008 8:57 am    Post subject: Reply with quote

PROBLEM SOLVED:

thank you "the_undead".

_________________
Code:
XXXXXX      XXXXXX
   XXXXX  XXXXX
     XXXXXXXX
    D I R E C T
     XXXXXXXX
   XXXXX  XXXXX
XXXXXX      XXXXXX
      GameDev
Back to top
View user's profile Send private message
Losplagos
Expert Cheater
Reputation: 0

Joined: 21 Mar 2006
Posts: 172
Location: Knee deep in a c++ book

PostPosted: Thu Jan 31, 2008 9:42 am    Post subject: Reply with quote

Global variables are a bad way to fix things.
Code:

#include <iostream>
#include <conio.h>
using std::cout;
using std::cin;
using std::endl;

class PLAYER
    {
          public:
           void sethealth(int num){health = num;};
           int gethealth() {return health;}
        private:
                 int health;
    };
int main()
    {
         PLAYER player;
        int gethp;
        cout << "Enter your health: ";
        cin >> gethp;
        player.sethealth(gethp);
      cout << "Your health is " << player.gethealth();
        _getch();
        return EXIT_SUCCESS;
   }


Lines 10, 11, and 22 have been edited. Simple'st solution.

_________________

Earthbound = 31337
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Jan 31, 2008 5:54 pm    Post subject: Reply with quote

You can also add the constructor to the class and have it set the health to 100 by default when the class is initialized:

Code:
class cPlayer
{
public:
   cPlayer()
   {
      plrHealth = 100;
   }
   int   GetPlayerHealth( )
   {
      return plrHealth;
   }
   void SetPlayerHealth( int iHealth )
   {
      plrHealth = iHealth;
   }

private:
   int plrHealth;
};

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
ShurikeN
Advanced Cheater
Reputation: 0

Joined: 09 Jan 2008
Posts: 84

PostPosted: Fri Feb 01, 2008 12:39 am    Post subject: Reply with quote

ok thanks everyone!
_________________
Code:
XXXXXX      XXXXXX
   XXXXX  XXXXX
     XXXXXXXX
    D I R E C T
     XXXXXXXX
   XXXXX  XXXXX
XXXXXX      XXXXXX
      GameDev
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites