| View previous topic :: View next topic |
| Author |
Message |
ShurikeN Advanced Cheater
Reputation: 0
Joined: 09 Jan 2008 Posts: 84
|
Posted: Thu Jan 31, 2008 8:39 am Post subject: C++ problem problem! |
|
|
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 |
|
 |
ThisIsImpossible Cheater
Reputation: 0
Joined: 06 Dec 2007 Posts: 28
|
Posted: Thu Jan 31, 2008 8:45 am Post subject: ... |
|
|
Click in error debug or something and it takes you automaticly for wrong code Then u see. |
|
| Back to top |
|
 |
the_undead Expert Cheater
Reputation: 1
Joined: 12 Nov 2006 Posts: 235 Location: Johannesburg, South Africa
|
Posted: Thu Jan 31, 2008 8:46 am Post subject: |
|
|
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 |
|
 |
ShurikeN Advanced Cheater
Reputation: 0
Joined: 09 Jan 2008 Posts: 84
|
Posted: Thu Jan 31, 2008 8:57 am Post subject: |
|
|
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 |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Thu Jan 31, 2008 9:42 am Post subject: |
|
|
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Jan 31, 2008 5:54 pm Post subject: |
|
|
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 |
|
 |
ShurikeN Advanced Cheater
Reputation: 0
Joined: 09 Jan 2008 Posts: 84
|
Posted: Fri Feb 01, 2008 12:39 am Post subject: |
|
|
ok thanks everyone! _________________
| Code: | XXXXXX XXXXXX
XXXXX XXXXX
XXXXXXXX
D I R E C T
XXXXXXXX
XXXXX XXXXX
XXXXXX XXXXXX
GameDev
|
|
|
| Back to top |
|
 |
|