I've been at this for a few hours now. I am making a small game in C++ and I am trying to figure out how I can edit variables from inside of a function. I can place the variables in the main() and edit them them there no problem, but I can't figure out what I need to do to edit from a function. Below is the function.
void HeroSelect(string& hero)
{
int gold = 20, health = 5, attack = 5, stats[2] = { health, attack };
string weapon;
cout << "Please choose your hero.\n";
cin >> hero;
if (hero == "Warrior" || hero == "warrior")
{
weapon = "Broad Sword";
cout << "You are a Warrior wielding a " << weapon << endl;
cout << "Good choice.\n"
<< "You start out with " << gold << " gold." << endl
<< "You have " << stats[0]<< " health and " << stats[1] << " attack.\n";
}
Above is the function definition and I want to be able to edit the health when the player gets attacked or say gets a stronger weapon. Below is the main() of the script.
void main()
{
double enter, room1, room2, room3, room4, BossRoom;
int, gold = 20; //This is used as part of check inventory and will be subtracted when necessary
string hero, weapon;
//Set decimal two places
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
Intro(enter);
cout << "The first step in this adventure is to choose your hero. You can choose a warrior, a wizard, or an archer.\n";
HeroSelect(hero);
So let's say he gets a new weapon and it gives +1 attack how do I reflect that in the function? Would I create a separate function for this?
As the player goes through the game, they can type in "stats" whenever, and when they do I want it to display their current health and attack and then essentially just loop back to the beginning of the room or fight they are in.
I know I can make the changes to the variables if I made all of my loops and all of my if-else statements in main(), but I wanted to try this and keep my main script clean. I did stats as an array, because I think it is good to print the array as a list. Any help would be great. Thanks!
Aucun commentaire:
Enregistrer un commentaire