Can somone please help me, Player class wont work for level system

closed account (LAfSLyTq)
here is my code...

#include <iostream>
#include <string>
using namespace std;

class Player
{
public:

int health;
int exp;
int level;
Player(bool)
{
health = 10;
level = 1;
exp = 0;
}

}

class Enemy
{
public:

int health;
int exp;
int level;
Enemy(bool)
{
health = 50;
}
};


int main()
{
string name;

char Action = ' ';

int money;
money=1000;

cout<<Player.level<<endl;
cout<<"Enter the amount of Player.exp you would like to gain."<<endl;
cin>>Player.exp;
{
if(Player.exp>=0 && Player.exp<99)
{
Player.level=1;
cout<<"Congratulations, you are now Player.level "<<Player.level<<"!"<<endl;
}
if(Player.exp>=100 && Player.exp<199)
{
Player.level=2;
cout<<"Congratulations, you are now Player.level "<<Player.level<<"!"<<endl;
}
if(Player.exp >=200 && Player.exp <299)
{
Player.level=3;
cout<<"Congratulations, you are now Player.level "<<Player.level<<"!"<<endl;
}
if(Player.exp>=300 && Player.exp<399)
{
Player.level=4;
cout<<"Congratulations, you are now Player.level "<<Player.level<<"!"<<endl;
}
if(Player.exp>=400 && Player.exp<499)
{
Player.level=5;
cout<<"Congratulations, you are now Player.level "<<Player.level<<"!"<<endl;
}
if(Player.exp>=500 && Player.exp<999)
{
Player.level=6;
cout<<"Congratulations, you are now Player.level "<<Player.level<<"!"<<endl;
}
if(Player.exp>=1000 && Player.exp<1999)
{
Player.level=7;
cout<<"Congratulations, you are now Player.level "<<Player.level<<"!"<<endl;
}
}
cout<<Player.level<<endl;
system ("pause");

}


I am getting the error everywhere that begins with "Player."
please point me out the problem...
Player is a class. It describes a new kind of object.

Do you understand why the following code doesn't work?
int = 7;
and why this code does work?
int x = 7;
Last edited on
When declaring a class, you just tell the compiler what an object of type "Player" looks like, you don't actually create any class instances (objects).

1
2
Player player; //creates an object called "player" of class type "Player"
player.exp; //now this works 


Also, you can greatly shorten the rest of your code by using integer division, for example:

1
2
3
4
5
const auto prevLevel=player.level;
if (player.exp<500)player.level=player.exp/100+1;
else if (player.exp<1000)player.level=6;
else player.level=7;
if (prevLevel!=player.level)cout << "Congratulations, you are now level " << player.level << "!" << endl;


You're also missing a semicolon after the Player class declaration and the gaining experience routine should be a member function of Player.
Last edited on
closed account (LAfSLyTq)
so what am i supposed to change?
You're supposed to make an object of type Player, and use that.
closed account (LAfSLyTq)
im sorry but i dont know how to do that... can you give me an example and where would i place it in the code?
Here is how to make an object of type int, named x.

int x;

Here is how to make an object of type float, named x.

float x;

Here is how to make an object of type beansOnToast, named x

beansOnToast x;

So how do you think you could make an object of type Player? Hint: Athar already did it for you.
I really think you oughta revise how classes work. Just knowing where to put the line that's going to solve this and understanding why it solves it are a gulf apart.
closed account (LAfSLyTq)
Athur said
Player player;
but idk where to put it. and also, it puts a red underline under
the first "Player"
closed account (LAfSLyTq)
nevermind i fixed it, thanks alot for your help guys. you're the best! ^.^
Topic archived. No new replies allowed.