#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...
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
constauto prevLevel=player.level;
if (player.exp<500)player.level=player.exp/100+1;
elseif (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.
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.