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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

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

Post 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
Last edited by epicasian on Mon Mar 22, 2010 6:35 pm, edited 1 time in total.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post by avansc »

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


you need to look at what the preprocessor does.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

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

Post 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
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

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

Post 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);
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

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

Post 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
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

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

Post 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.
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

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

Post by epicasian »

It's cool that this forum has so many awesome members :D
Post Reply