-
Whoa... tough one here.
I'm wanting to add a levelUp function to my code, but I'm not exactly sure how to go about that. I'll post the code real quick and continue why...
Code:
#include <iostream>
#include <string>
using namespace std;
//START STATS CLASS
class Stats
{
public:
Stats();
~Stats();
void setStrength(int str);
int showStrength() const;
void setDefense(int def);
int showDefense() const;
void setSpeed(int spd);
int showSpeed() const;
void setMagic(int mag);
int showMagic() const;
private:
int strength;
int defense;
int speed;
int magic;
};
//END STATS CLASS
//STATS FUNCTIONS DECLARACTIONS START
Stats::Stats(){};
Stats::~Stats(){};
void Stats::setStrength(int str) { strength = str; }
int Stats::showStrength() const { return strength; }
void Stats::setDefense(int def) { defense = def; }
int Stats::showDefense() const { return defense; }
void Stats::setSpeed(int spd) { speed = spd; }
int Stats::showSpeed() const { return speed; }
void Stats::setMagic(int mag) { magic = mag; }
int Stats::showMagic() const { return magic; }
//STATS FUNCTIONS DECLARATIONS END
int main()
{
//**variables**//
string charName;
string raceChoice;
unsigned short int raceCount = 0;
string charRace;
//**variables**//
//START STATS
Stats * dwarf = new Stats; //dwarf stats
dwarf->setStrength(12);
dwarf->setDefense(10);
dwarf->setSpeed(5);
dwarf->setMagic(2);
Stats * elf = new Stats; //elf stats
elf->setStrength(2);
elf->setDefense(5);
elf->setSpeed(7);
elf->setMagic(14);
Stats * human = new Stats; //human stats
human->setStrength(7);
human->setDefense(6);
human->setSpeed(8);
human->setMagic(7);
Stats * player = new Stats; //player stats will be set later
//END STATS
//start the race and stats listing
cout<<"\t.::Race List::.\n\n";
cout<<"Race:\t\tDwarf\t\tElf\t\tHuman\n"; //races
cout<<"Strength:\t" //begin strength
<<dwarf->showStrength()
<<"\t\t"
<<elf->showStrength()
<<"\t\t"
<<human->showStrength()
<<endl;
cout<<"Defense:\t" //begin defense
<<dwarf->showDefense()
<<"\t\t"
<<elf->showDefense()
<<"\t\t"
<<human->showDefense()
<<endl;
cout<<"Speed:\t\t" //begin speed
<<dwarf->showSpeed()
<<"\t\t"
<<elf->showSpeed()
<<"\t\t"
<<human->showSpeed()
<<endl;
cout<<"Magic:\t\t" //begin magic
<<dwarf->showMagic()
<<"\t\t"
<<elf->showMagic()
<<"\t\t"
<<human->showMagic()
<<endl;
cout<<"\n";
//end the race and stats listing
//begin race selection
while (raceCount < 1)
{
cout<<"What race will you be born?\n";
cout<<"Choice: ";
cin>>raceChoice;
cin.ignore();
if ((raceChoice == "Dwarf") || (raceChoice == "dwarf"))
{
//player stats if dwarf is chosen
player->setStrength( dwarf->showStrength() );
player->setDefense( dwarf->showDefense() );
player->setSpeed( dwarf->showSpeed() );
player->setMagic( dwarf->showMagic() );
charRace="Dwarf";
raceCount++;
}
else if ((raceChoice == "Elf") || (raceChoice == "elf"))
{
//player stats if elf is chosen
player->setStrength( elf->showStrength() );
player->setDefense( elf->showDefense() );
player->setSpeed( elf->showSpeed() );
player->setMagic( elf->showMagic() );
charRace="Elf";
raceCount++;
}
else if ((raceChoice == "Human") || (raceChoice == "human"))
{
//player stats if human is chosen
player->setStrength( human->showStrength() );
player->setDefense( human->showDefense() );
player->setSpeed( human->showSpeed() );
player->setMagic( human->showMagic() );
charRace="Human";
raceCount++;
}
else
{
cout<<"\nThat is not a valid race.\n\n";
}
}
//end race selection
//begin name selection
cout<<"\nOkay, "<<charRace<<", now what is your name?\n";
cout<<"Name: ";
cin>>charName;
cin.ignore();
//end name selection
//begin player stats
cout<<"\n\n";
cout<<"Name:\t\t"<<charName<<endl;
cout<<"Strength:\t"<<player->showStrength()<<endl;
cout<<"Defense:\t"<<player->showDefense()<<endl;
cout<<"Speed:\t\t"<<player->showSpeed()<<endl;
cout<<"Magic:\t\t"<<player->showMagic()<<endl;
cout<<"\n";
//end player stats
//FREE MEMORY ALLOCATED
delete dwarf;
delete elf;
delete human;
delete player;
//FREE MEMORY ALLOCATED
cin.get();
return 0;
}
What I'm wanting to do is make a function, preferably a method of Stats, that calls player->showStrength and the other stat names and add a certain number to each. Also, for every race, the value that is added to the stat needs to be different.
Example:
Race: Dwarf
Strength + 3;
Defense + 2;
Speed + 1;
Magic + 1;
Race: Elf
Strength + 1;
Defense + 1;
Speed + 2;
Magic + 4;
And something else for humans. My main problem is that the whole Stats * player = new Stats hasn't been defined yet. What should I do?
Oh, and I've thought about doing all of that in the constructor of Stats. Would that work?
Please, tips and even some coding would help me out a bunch. Thanks in advance.
-
You're going about a bunch of things wrong in your code. First of all, you're making a new instance of Stats for EVERY race when the application starts. This is very bad for memory. You should make the new instance for ONLY the race they choose. So if they choose a dwarf - an elf, human, orc etc. won't be in memory. I also think you should make a race class to manage the race data as well. There you could make a LevelUp function to control a race's certain stats that need to be incremented. A structure would be nice to control those stats.
Code:
struct OrcStats
{
Strength = 5;
Intelligence = 1;
etc...
};
load a structure like that into your race class upon selection and use it for incrementing the players stats. Another way is making 'cRace' an abstract class then branching the different races off as derivatives of 'cRace', each having overriden functions. Of course, there are many ways to do just about everything in C++. That should get you moving in a more OOP approach though.
-
Hmm, okay I think I need to read some more before I attempt this. Thanks for those tips, though. Before I go off to reading again... you're saying instead of making an instance for each race, I should just do the player instance and depending on my race class, configure them?
edit: This better man?
Code:
#include <iostream>
#include <string>
using namespace std;
//START STATS CLASS
class Stats
{
public:
Stats();
~Stats();
void setStrength(int str);
int showStrength() const;
void setDefense(int def);
int showDefense() const;
void setSpeed(int spd);
int showSpeed() const;
void setMagic(int mag);
int showMagic() const;
private:
int strength;
int defense;
int speed;
int magic;
};
//END STATS CLASS
//STATS FUNCTIONS DECLARACTIONS START
Stats::Stats(){};
Stats::~Stats(){};
void Stats::setStrength(int str) { strength = str; }
int Stats::showStrength() const { return strength; }
void Stats::setDefense(int def) { defense = def; }
int Stats::showDefense() const { return defense; }
void Stats::setSpeed(int spd) { speed = spd; }
int Stats::showSpeed() const { return speed; }
void Stats::setMagic(int mag) { magic = mag; }
int Stats::showMagic() const { return magic; }
//STATS FUNCTIONS DECLARATIONS END
int main()
{
//**variables**//
string charName;
string raceChoice;
unsigned short int raceCount = 0;
string charRace;
//**variables**//
//begin race selection
Stats * player = new Stats;
while (raceCount < 1)
{
cout<<"Dwarf\tElf\tHuman\n\n";
cout<<"What race will you be born?\n";
cout<<"Choice: ";
cin>>raceChoice;
cin.ignore();
if ((raceChoice == "Dwarf") || (raceChoice == "dwarf"))
{
//player stats if dwarf is chosen
player->setStrength( 12 );
player->setDefense( 10 );
player->setSpeed( 5 );
player->setMagic( 2 );
charRace="Dwarf";
raceCount++;
}
else if ((raceChoice == "Elf") || (raceChoice == "elf"))
{
//player stats if elf is chosen
player->setStrength( 2 );
player->setDefense( 5 );
player->setSpeed( 7 );
player->setMagic( 14 );
charRace="Elf";
raceCount++;
}
else if ((raceChoice == "Human") || (raceChoice == "human"))
{
//player stats if human is chosen
player->setStrength( 7 );
player->setDefense( 6 );
player->setSpeed( 8 );
player->setMagic( 7 );
charRace="Human";
raceCount++;
}
else
{
cout<<"\nThat is not a valid race.\n\n";
}
}
//end race selection
//begin name selection
cout<<"\nOkay, "<<charRace<<", now what is your name?\n";
cout<<"Name: ";
cin>>charName;
cin.ignore();
//end name selection
//begin player stats
cout<<"\n\n";
cout<<"Name:\t\t"<<charName<<endl;
cout<<"Strength:\t"<<player->showStrength()<<endl;
cout<<"Defense:\t"<<player->showDefense()<<endl;
cout<<"Speed:\t\t"<<player->showSpeed()<<endl;
cout<<"Magic:\t\t"<<player->showMagic()<<endl;
cout<<"\n";
//end player stats
//FREE MEMORY ALLOCATED
delete player;
//FREE MEMORY ALLOCATED
cin.get();
return 0;
}
And about the whole load the structure thing into the class, I'm not that good yet, or I don't understand exactly what you mean. Thanks again.
-
Much Better! The only Stats initialized is the one you need. Now, what you need to do to make the level's go up according to the race they choose. So, do this for all lines of creation:
Code:
if ((raceChoice == "Dwarf") || (raceChoice == "dwarf"))
{
//player stats if dwarf is chosen
player->setStrength( 12 );
player->setDefense( 10 );
player->setSpeed( 5 );
player->setMagic( 2 );
charRace="Dwarf";
raceCount++;
//NEW CODE
struct StatIncrement
{
int iStrength = 5;
int iIntelligence = 2;
blah blah
};
}
now you need to make a new function to add to the stats, and when they level you can pass it in as follows
Code:
//EXAMPLE
addStrength(StatIncrement.iStrength);
That's just one way to do it. Play around with it and figure out a way to add to all the stats with 1 function by passing in the whole struct! You could do this for setting all the stats also, this should give you some much cleaner code.
-
Hmm, so you can actually create a structure within a function? So like, if they choose dwarf the StatIncrement.iStrength inside the struct would increase the stats in the class with the function? Would I need to use references for that?
edit: I added the struct to it, and uh... I got the errors:
Code:
ISO C++ forbids initialization of member `iStrength'
making `iStrength' static
ISO C++ forbids in-class initialization of non-const static member `iStrength'
local class `struct main()::StatIncrement' shall not have static data member `int main()::StatIncrement::iStrength'
all on the same line... so I can't declare a structure within the main function?
-
Alright, new problem. I didn't do the structure thing, instead I just made a levelUp function.
However, when I try to put in the strength and stats values, I, er... get a compiler error.
Here's my code:
Code:
#include <iostream>
#include <string>
using namespace std;
//START STATS CLASS
class Stats
{
public:
Stats();
~Stats();
void setStrength(int str);
int showStrength() const;
void setDefense(int def);
int showDefense() const;
void setSpeed(int spd);
int showSpeed() const;
void setMagic(int mag);
int showMagic() const;
private:
int strength;
int defense;
int speed;
int magic;
};
//END STATS CLASS
//STATS FUNCTIONS DECLARACTIONS START
Stats::Stats(){};
Stats::~Stats(){};
void Stats::setStrength(int str) { strength = str; }
int Stats::showStrength() const { return strength; }
void Stats::setDefense(int def) { defense = def; }
int Stats::showDefense() const { return defense; }
void Stats::setSpeed(int spd) { speed = spd; }
int Stats::showSpeed() const { return speed; }
void Stats::setMagic(int mag) { magic = mag; }
int Stats::showMagic() const { return magic; }
//STATS FUNCTIONS DECLARATIONS END
//levelUp function start
void levelUp(int, int, int, int);
void levelUp(int &riStrength, int &riDefense, int &riSpeed, int &riMagic, string &rRace)
{
if (rRace == "Dwarf") //about to incriment differently for each race
{
riStrength += 3;
riDefense += 2;
riSpeed += 2;
riMagic += 1;
}
else if (rRace == "Elf")
{
riStrength +=1;
riDefense += 2;
riSpeed += 2;
riMagic += 3;
}
else if (rRace == "Human")
{
riStrength += 2;
riDefense += 2;
riSpeed +=2;
riMagic +=2;
}
}
/*
NOTE: MUST LEVEL UP AS levelUp(
player->showStrength,
player->showDefense,
player->showSpeed,
player->showMagic,
charRace);
*/
//levelUp function end
int main()
{
//**variables**//
string charName;
string raceChoice;
unsigned short int raceCount = 0;
string charRace;
//**variables**//
//begin race selection
Stats * player = new Stats;
while (raceCount < 1)
{
cout<<"Dwarf\tElf\tHuman\n\n";
cout<<"What race will you be born?\n";
cout<<"Choice: ";
cin>>raceChoice;
cin.ignore();
if ((raceChoice == "Dwarf") || (raceChoice == "dwarf"))
{
//player stats if dwarf is chosen
player->setStrength( 12 );
player->setDefense( 10 );
player->setSpeed( 5 );
player->setMagic( 2 );
charRace="Dwarf";
raceCount++;
}
else if ((raceChoice == "Elf") || (raceChoice == "elf"))
{
//player stats if elf is chosen
player->setStrength( 2 );
player->setDefense( 5 );
player->setSpeed( 7 );
player->setMagic( 14 );
charRace="Elf";
raceCount++;
}
else if ((raceChoice == "Human") || (raceChoice == "human"))
{
//player stats if human is chosen
player->setStrength( 7 );
player->setDefense( 6 );
player->setSpeed( 8 );
player->setMagic( 7 );
charRace="Human";
raceCount++;
}
else
{
cout<<"\nThat is not a valid race.\n\n";
}
}
//end race selection
//begin name selection
cout<<"\nOkay, "<<charRace<<", now what is your name?\n";
cout<<"Name: ";
cin>>charName;
cin.ignore();
//end name selection
//begin player stats
cout<<"\n\n";
cout<<"Name:\t\t"<<charName<<endl;
cout<<"Strength:\t"<<player->showStrength()<<endl;
cout<<"Defense:\t"<<player->showDefense()<<endl;
cout<<"Speed:\t\t"<<player->showSpeed()<<endl;
cout<<"Magic:\t\t"<<player->showMagic()<<endl;
cout<<"\n";
//end player stats
//begin leveling up
levelUp(player->showStrength,player->showDefense,player->showSpeed,player->showMagic,charRace);
//end leveling up
//FREE MEMORY ALLOCATED
delete player;
//FREE MEMORY ALLOCATED
cin.get();
return 0;
}
I know it has something to do with my references, but I can't pinpoint it. Any tips?
-
yea I'm sorry you need to global that struct. As for your new problem, can you post some compiler errors, maybe point me in the direction of what line it is on? It's probably just a missing ; or something.
-
I see one thing.. its suppose to be:
Code:
void levelUp(int&, int&, int&, int&, string&);
P.S. You could use a double enumerator for the class chosen, which makes the code more clear, and you dont have to pass a string containing the chosen class around. Of course since you probably planned on printing the class chosen in the game anyway, it would be necessary to have that string anyway.
-
Okay, I added the reference operator to the string. Here are my compiler errors.
invalid initialization of reference of type 'int&' from expression of type '<unknown type>'
passing argument 1 of `void levelUp(int&, int&, int&, int&, std::string&)'
What do I do?
-
Okay, I didn't put the parenthesis in the showStrength thing where I pass the arguments for levelUp()'s paremeters, but I'm still getting the same problem. Here's the update of my code. What in the world is wrong?! :)
Code:
#include <iostream>
#include <string>
using namespace std;
//START STATS CLASS
class Stats
{
public:
Stats();
~Stats();
void setStrength(int str);
int showStrength();
void setDefense(int def);
int showDefense();
void setSpeed(int spd);
int showSpeed();
void setMagic(int mag);
int showMagic();
private:
int strength;
int defense;
int speed;
int magic;
};
//END STATS CLASS
//STATS FUNCTIONS DECLARACTIONS START
Stats::Stats(){};
Stats::~Stats(){};
void Stats::setStrength(int str) { strength = str; }
int Stats::showStrength(){ return strength; }
void Stats::setDefense(int def) { defense = def; }
int Stats::showDefense(){ return defense; }
void Stats::setSpeed(int spd) { speed = spd; }
int Stats::showSpeed(){ return speed; }
void Stats::setMagic(int mag) { magic = mag; }
int Stats::showMagic(){ return magic; }
//STATS FUNCTIONS DECLARATIONS END
//levelUp function start
void levelUp(int &, int &, int &, int &, string);
void levelUp(int &riStrength, int &riDefense, int &riSpeed, int &riMagic, string rRace)
{
if (rRace == "Dwarf") //about to incriment differently for each race
{
riStrength += 3;
riDefense += 2;
riSpeed += 2;
riMagic += 1;
}
else if (rRace == "Elf")
{
riStrength +=1;
riDefense += 2;
riSpeed += 2;
riMagic += 3;
}
else if (rRace == "Human")
{
riStrength += 2;
riDefense += 2;
riSpeed +=2;
riMagic +=2;
}
}
/*
NOTE: MUST LEVEL UP AS levelUp(
player->showStrength,
player->showDefense,
player->showSpeed,
player->showMagic,
charRace);
*/
//levelUp function end
int main()
{
//**variables**//
string charName;
string raceChoice;
unsigned short int raceCount = 0;
string charRace;
//**variables**//
//begin race selection
Stats * player = new Stats;
while (raceCount < 1)
{
cout<<"Dwarf\tElf\tHuman\n\n";
cout<<"What race will you be born?\n";
cout<<"Choice: ";
cin>>raceChoice;
cin.ignore();
if ((raceChoice == "Dwarf") || (raceChoice == "dwarf"))
{
//player stats if dwarf is chosen
player->setStrength( 12 );
player->setDefense( 10 );
player->setSpeed( 5 );
player->setMagic( 2 );
charRace="Dwarf";
raceCount++;
}
else if ((raceChoice == "Elf") || (raceChoice == "elf"))
{
//player stats if elf is chosen
player->setStrength( 2 );
player->setDefense( 5 );
player->setSpeed( 7 );
player->setMagic( 14 );
charRace="Elf";
raceCount++;
}
else if ((raceChoice == "Human") || (raceChoice == "human"))
{
//player stats if human is chosen
player->setStrength( 7 );
player->setDefense( 6 );
player->setSpeed( 8 );
player->setMagic( 7 );
charRace="Human";
raceCount++;
}
else
{
cout<<"\nThat is not a valid race.\n\n";
}
}
//end race selection
//begin name selection
cout<<"\nOkay, "<<charRace<<", now what is your name?\n";
cout<<"Name: ";
cin>>charName;
cin.ignore();
//end name selection
//begin player stats
cout<<"\n\n";
cout<<"Name:\t\t"<<charName<<endl;
cout<<"Strength:\t"<<player->showStrength()<<endl;
cout<<"Defense:\t"<<player->showDefense()<<endl;
cout<<"Speed:\t\t"<<player->showSpeed()<<endl;
cout<<"Magic:\t\t"<<player->showMagic()<<endl;
cout<<"\n";
//end player stats
//begin leveling up
levelUp(player->showStrength(),player->showDefense(),player->showSpeed(),player->showMagic(),charRace);
//end leveling up
//FREE MEMORY ALLOCATED
delete player;
//FREE MEMORY ALLOCATED
cin.get();
return 0;
}
-
I was just showing that the function, and the function prototype should have the same parameter type, references and/or pointers.
It would be better if you passed a reference to your class object, for future modifications may lead to added stats. It would solve your problems with the string, or just deep copying it like you are doing now works. There might be a special way to pass a string, I dont know.. I havent been programming in a while.
References are wierd on initialization, I forget why, but pointers are easy.. this was like in tutorial #3. Change & to *, and add * to variables (e.g. *riStrength += 3;).