简体   繁体   中英

Too Many Arguments in C++

I am facing difficulties in my C++ code. I am a beginner. Like, only with very basic knowledge of C++. So, I really can't figure a way to do this. I thought of making an RPG game using C++ commands and am close to finishing it. But somehow, I couldn't make a constant health for the hero. Taking a look at the code,

#include <iostream>
#include <cstdlib>
#include <ctime>



using namespace std;


class player
{   public:
    int health = 100;
};

int battle();
void death();

int main()
{
   int abc;
   player hero;
   hero.health = abc;
   int a;
   int replay = 1;
   cout << "You have 100 Hp. \n";
   while (replay == 1)
{

srand(time(0));
cout << "\n Press 1 to move forward; 2 To stay. \n";
cin >> a;
    if (a == 2)
    {
        if (rand() % 4 + 1 != 1)
        {
            cout << "You stay at your place. \n";
        }
        else
        {
            cout << "Enemy Attacks! (20 Hp) \n";
            //battle(hero.health);
            //cout << "\n Press 1 to continue. \n";
            cout << "\n Do you want to play again? Press 1 to replay and 0 to quit.\n";
            cin >> replay;
        }
    }
    else if (a == 1)
    {
        if (rand() % 2 + 1 != 1)
        {
            cout << "You moved forward. No one around. \n";
        }
        else
        {
            cout << "You move forward. Enemy attacks! (20 Hp) \n";
            battle(abc);
            cout << "\n Do you want to play again? Press 1 to replay and 0 to quit.\n";
            cin >> replay;
        }
    }
    else
    {
        cout << "Sorry. Please enter a valid move. \n";
    }

}

return 0;
}

int battle(int x)
{
   player enemy;
   enemy.health = 20;
   player hero;
   int y;


while (enemy.health >= 0)
{
int eattack = rand() % 15 + 7;
int attack = rand() % 10 + 1;
int escape = rand() % 4 + 1;
cout << "\n Press 1 to attack. 2 to flee \n";
cin >> y;
if (y == 2)
    {
      if (escape != 1)
      {
        cout << "Can't escape! \n";
        cout << "Enemy attacked! Dealing a damage of: " << eattack << " Hp. \n";
        hero.health = hero.health - eattack;
        cout << "Your Hp is: " << hero.health;
      }
      else
      {
        goto Aftermath;
      }

    }
else if (y != 1)
    {
        cout << "Sorry. Please enter a valid response. \n";
    }
else
    {
        cout << "You attack the enemy. \n";
        cout << "You deal a damage of: " << attack;
        enemy.health = enemy.health - attack;
        if (enemy.health >= 0)
        {
            cout << "\n Enemy attacks you, dealing: " << eattack << " Hp damage.";
            hero.health = hero.health - eattack;
            cout << "\n You have: " << hero.health << " Hp left.";
        }
    }

if ((hero.health <= 0) || (hero.health == 0))
    {
        death();
        enemy.health = -1;
    }
}

if (hero.health > 0)
    {
        cout << "\n Enemy fainted!";
        //cout << "You found Hp Potion! Your Hp was refilled.";
    }

   Aftermath:
      if ((hero.health > 0) && (enemy.health > 0))
       {
          cout << "Escaped Successfully! \n";
       }

     return x;

  }
 void death()
    {
      cout << "You died!";
    }

As you see, I have called for battle(abc) and battle(hero.health) [which I have commented for now] but the problem is, it says "Too many arguments to function int battle() . Previously, I simply avoided parameters and created object "hero" in the battle method itself. But every time you get through a battle sequence, it comes back and declares it again, thus making its health refill. [Look at if (hero.health > 0) ]

I really don't know about global variables and all that. I just want to know if there is a workaround or a way to solve this parameter problem. Any other suggestions to have health as a 'constant' and not declared every time is also warmly accepted. Thank you so much!

PS Suggestions for shortening the code also accepted but please, I am a beginner. So advanced strategies are beyond my skills right now. I take time to grasp concepts.

You declare the function before the main method, and then you implement the function after the main method.

The problem is, you implement the function as:

 int battle(int x)

but this doesn't match your declaration:

 int battle();

Just change your function declaration block so the battle function matches the expected signature:

int battle(int x);
void death();

That will get your code compiling, but you are still a number of steps from getting this to work.

I'll give you one starter: instead of passing in the hitpoints into battle , pass the entire player.

 void battle(Player &player)
 {
     // ...
 }

Then you can modify the player's hitpoints directly in the battle function.

You would then call this with:

 battle(hero);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM