Page 1 of 1

[SOLVED]...functions being run when I haven't called them?

Posted: Mon Mar 22, 2010 5:19 pm
by epicasian
Ok, I've tried to figure this one out for about 30 minutes to an hour now and couldn't come up with a solution.

Basically, in my main file, I'm including a file(game.h) that has functions to retrieve members of instances in my classes. The problem is that the functions are being run, and the instances of the class' members are being edited when I don't want them to yet. I'm sorry if this sounds confusing.

main.cpp:

Code: Select all

#include <iostream>
#include "game.h"


int main(int argc, char *args[])
{
    using namespace std;
    cin.get();
    return 0; 
}
game.h:

Code: Select all

//game.h functions
#include "enemy.h" 
#include "player.h"
#include <iostream>
#include <string>

using namespace std;

   player newPlayer;
    enemy newEnemy;

    int *enemyHealth = newEnemy.ptrToHP();
    int *playerHealth = newPlayer.ptrToHP();
    int *playerPotions = newPlayer.getPotions();
    int *enemyPotions = newEnemy.getPotions(); 
    int enemyAttack = newEnemy.attack(newPlayer.ptrToHP());
    int  checkHP(); 
    int playerAttack = newPlayer.attack(enemyHealth);
    
    string choice;

   int playerChoice()
{
    cout << "Type 'attack' to attack or 'heal' to use a potion..." << endl;
    cin >> choice;

    if(choice == "attack")
              {newPlayer.attack(enemyHealth);
              cout << endl << *enemyHealth;}
    else if(choice == "heal")
         {cout << endl << "The choice was heal";}   
}
int enemyAttackTurn()
{
           cout << "\nNameless' Turn:\n";   
           newEnemy.attack(newPlayer.ptrToHP());
           checkHP();
           
}
int enemyHealTurn()
{
               cout << "\nNameless attempts to use a potion...\n";
               srand(time(NULL));
               int randomNumber = rand() % 10;
               if (randomNumber <= 60 && newEnemy.getPotions()== 0)
                  {cout << "but has no potions left!";}
               else if (randomNumber <= 60)
                  {cout << "and heals 10 HP";
               enemyHealth = enemyHealth + 10;}
               else 
                  {cout << "and it fails!";}
}
int startBattle()
{
          enemyAttackTurn();   
          playerChoice();
          
}
int checkHP()
{
    if(newPlayer.ptrToHP() <= 0)
    {
      cout << "Press any key to exit...";
      cin.get();
      abort();
    }
}
The Output:

Code: Select all

Nameless is attacking and dealt 7 damage!

Player is attacking and dealt 57 damage!
Thank you!

EpicAsian

Re: Why are my functions being run when I haven't called them?

Posted: Mon Mar 22, 2010 5:34 pm
by avansc
int enemyAttack = newEnemy.attack(newPlayer.ptrToHP());
int playerAttack = newPlayer.attack(enemyHealth);


you need to look at what the preprocessor does.

Re: Why are my functions being run when I haven't called them?

Posted: Mon Mar 22, 2010 5:47 pm
by epicasian
I'm sorry, but I don't get what you mean by:
avansc wrote: you need to look at what the preprocessor does.
Thanks again,
EpicAsian

Re: Why are my functions being run when I haven't called them?

Posted: Mon Mar 22, 2010 6:13 pm
by hurstshifter
I think avansc was trying to point out that on these two lines of code, you are actually executing the attack() functions. Did you not intend to do this and what were you expecting from these procedures?

Code: Select all

  int enemyAttack = newEnemy.attack(newPlayer.ptrToHP());

    int playerAttack = newPlayer.attack(enemyHealth);

Re: Why are my functions being run when I haven't called them?

Posted: Mon Mar 22, 2010 6:22 pm
by epicasian
Thank you for pointing that out to me hurstshifter. I was trying to create a pointer (I think) to that function. I guess I was doing it wrong :) .

I'm still learning C++, so thank you guys for the help,
EpicAsian

Re: Why are my functions being run when I haven't called them?

Posted: Mon Mar 22, 2010 6:30 pm
by K-Bal
epicasian wrote: I'm still learning C++
It's not like you ever stop doing so. One of the many reasons to be registered in this forum.

Re: Why are my functions being run when I haven't called them?

Posted: Mon Mar 22, 2010 6:33 pm
by epicasian
It's cool that this forum has so many awesome members :D