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
epicasian
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
Post
by epicasian » Mon Mar 22, 2010 5:19 pm
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.
avansc
Respected Programmer
Posts: 1708 Joined: Sun Nov 02, 2008 6:29 pm
Post
by avansc » Mon Mar 22, 2010 5:34 pm
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"
epicasian
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
Post
by epicasian » Mon Mar 22, 2010 5:47 pm
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
hurstshifter
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:
Post
by hurstshifter » Mon Mar 22, 2010 6:13 pm
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);
epicasian
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
Post
by epicasian » Mon Mar 22, 2010 6:22 pm
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
Posts: 701 Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:
Post
by K-Bal » Mon Mar 22, 2010 6:30 pm
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.
epicasian
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
Post
by epicasian » Mon Mar 22, 2010 6:33 pm
It's cool that this forum has so many awesome members :D