Jump to content
  • Advertisement
Sign in to follow this  
InterFiction

Function

This topic is 2593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been bouncing back and forth from reading books, to getting help here, and cplusplus.com. I've been applying my new knowledge on the library I'm working on, slowly making it more useful. Finally now I understand pointers a bit better, and was able to make a function that in theory should actually do something, but right now it's not quite going the way I want it too...

I'm not sure but in main I call my function, and after it's called I output EnemyHealth. There doesn't seem to be any mathematical difference to what was put into the function.

Main

#include <iostream>
#include<string>
#include "OgreTitanium.h"
using namespace std;

int main()
{

int EnemyHealth;
int Damage = 50;
int * pEnemyHealth;
string EnemyName = "a rat";
pEnemyHealth = &EnemyHealth;
*pEnemyHealth = 100;

OgreRanSwordStrikesBtl(1,Damage,pEnemyHealth,EnemyName);

cout << EnemyHealth;

return 0;
}


OgreTitanium.H

#ifndef OGREWEAPONSTRIKES_H
#define OGREWEAPONSTRIKES_H
#include<string>
using std::string;
// this namespace is for holding classes and functions dealing with weapon strikes.

void OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int* pEnemyHealth, string EnemyName);



#endif // OGREWEAPONSTRIKES_


OgreTitanium.cpp

#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
#include<ctime>
using std::string;
using namespace std;

//***The following code is for battle weapon strike functions. These functions subtract damage, and return remaining enemy health.

//this is random sword strikes function. it's for random sword strikes.
void OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int* pEnemyHealth, string EnemyName)
{



if (OgreSwordStrikesID==1){
//random attack display will determine which text will be displayed for the attack message.
//if it's a 1 it will generate for light attacks, if it's a 2 it will generate for critical attacks.

int RandomAttackDisplay;
RandomAttackDisplay = (rand()%20)+1;


switch (RandomAttackDisplay){
pEnemyHealth-=DamageAmount;
case 1:
cout << "A light slash doing minimal damage of " << DamageAmount << " to " << EnemyName <<"." <<endl;
break;
case 2:
cout << "A light slash does minimal damage to " << EnemyName <<" " << DamageAmount <<"." << endl;
break;
case 3:
cout << "A light slash cuts " << EnemyName << " for " << DamageAmount <<" damage." << endl;
break;
case 4:
cout << "Sword grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 5:
cout << "Sword pokes " << EnemyName << " doing " << DamageAmount << " damage. " << endl;
break;
case 6:
cout << "Sword lightly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 7:
cout << "Blade grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 8:
cout << "Sword jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 9:
cout << "Sword lightly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 10:
cout << "Sword barely grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 11:
cout << "Blade hardly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 12:
cout << "Blade lightly jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 13:
cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 14:
cout << "Sword hardly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 15:
cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 16:
cout << "Blade barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 17:
cout << "Blade hardly cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 18:
cout << "Blade lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 19:
cout << "Sword lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 20:
cout << "Sword hardly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
default:
cout << "There has been an error." << endl << "This is an internal error" << endl;
cout << "please report the bug to creator." << endl;
}
}

else if (OgreSwordStrikesID==2)

{

cout << OgreSwordStrikesID;
}

}

Share this post


Link to post
Share on other sites
Advertisement
pEnemyHealth-=DamageAmount; adjusts the pointer, not the value of the pointer. You want to dereference the pointer first. *pEnemyHealth-=DamageAmount;

Share this post


Link to post
Share on other sites
OgreTitanium.CPP

hmm, like this? I'm still getting an output of 50...

using namespace std;

//***The following code is for battle weapon strike functions. These functions subtract damage, and return remaining enemy health.

//this is random sword strikes function. it's for random sword strikes.
void OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int* pEnemyHealth, string EnemyName)
{



if (OgreSwordStrikesID==1){
//random attack display will determine which text will be displayed for the attack message.
//if it's a 1 it will generate for light attacks, if it's a 2 it will generate for critical attacks.

int RandomAttackDisplay;
RandomAttackDisplay = (rand()%20)+1;


switch (RandomAttackDisplay){
*pEnemyHealth-=DamageAmount;
case 1:
cout << "A light slash doing minimal damage of " << DamageAmount << " to " << EnemyName <<"." <<endl;
break;
case 2:
cout << "A light slash does minimal damage to " << EnemyName <<" " << DamageAmount <<"." << endl;
break;
case 3:
cout << "A light slash cuts " << EnemyName << " for " << DamageAmount <<" damage." << endl;
break;
case 4:
cout << "Sword grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 5:
cout << "Sword pokes " << EnemyName << " doing " << DamageAmount << " damage. " << endl;
break;
case 6:
cout << "Sword lightly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 7:
cout << "Blade grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 8:
cout << "Sword jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 9:
cout << "Sword lightly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 10:
cout << "Sword barely grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 11:
cout << "Blade hardly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 12:
cout << "Blade lightly jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 13:
cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 14:
cout << "Sword hardly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 15:
cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 16:
cout << "Blade barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 17:
cout << "Blade hardly cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 18:
cout << "Blade lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 19:
cout << "Sword lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
case 20:
cout << "Sword hardly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;
default:
cout << "There has been an error." << endl << "This is an internal error" << endl;
cout << "please report the bug to creator." << endl;
}
}

else if (OgreSwordStrikesID==2)

{

cout << OgreSwordStrikesID;
}

}

Share this post


Link to post
Share on other sites
Also, move that line before the switch block.

Incidentally, you may want to turn up the warning level of your compiler. Ex: when MSVC is fed your code at warning level four it warns you that the line is unreachable.

Share this post


Link to post
Share on other sites
Hey thanks. I'm learning here :) . Decided to switch out for pass by reference to make everyone's life easier, but now I'm facing a logical error of some type. When the function is called from main it will only generate 1 message. It doesn't seem to be randomly generating a number for RandomAttackDisplay.

if my first argument in function is 1 at call time I get

case 2:
cout << "A light slash does minimal damage to " << EnemyName <<" " << DamageAmount <<"." << endl;
break;


if the first argument at call time is 2 for critical attacks I get this display.

case 2:
cout << "Sword slices " << EnemyName << " doing " << DamageAmount << " damage." << endl;
break;


sense it's the same case from either switch it must be the same problem.

error must be in this code at the head of the switch..

int RandomAttackDisplay;
RandomAttackDisplay = (rand()%20)+1;
EnemyHealth-=DamageAmount;

Share this post


Link to post
Share on other sites
I don't see you seeding the random number generator anywhere. You can think of the seed as choosing the list of numbers the PRNG will give you. In your case, and often, you want to call srand(time(0)) exactly once, at the start of the program. This seeds the PRNG with the current time. If you don't do this, you'll get the same sequence every time you run your program.

(This is assuming I understand you correctly.)

Share this post


Link to post
Share on other sites

I don't see you seeding the random number generator anywhere. You can think of the seed as choosing the list of numbers the PRNG will give you. In your case, and often, you want to call srand(time(0)) exactly once, at the start of the program. This seeds the PRNG with the current time. If you don't do this, you'll get the same sequence every time you run your program.

(This is assuming I understand you correctly.)


oh yeah, sorry about that. I had that in my function, and someone told me it was a bad idea, because it would seed every time the function was called. I forgot to add it back in. Where's the best place to do this in main? I only have a basic grasp of programming at the moment. Is the main function ever truly exited, and reentered?

Share this post


Link to post
Share on other sites

[quote name='TheUnbeliever' timestamp='1321159776' post='4883391']
I don't see you seeding the random number generator anywhere. You can think of the seed as choosing the list of numbers the PRNG will give you. In your case, and often, you want to call srand(time(0)) exactly once, at the start of the program. This seeds the PRNG with the current time. If you don't do this, you'll get the same sequence every time you run your program.

(This is assuming I understand you correctly.)


oh yeah, sorry about that. I had that in my function, and someone told me it was a bad idea, because it would seed every time the function was called. I forgot to add it back in. Where's the best place to do this in main? I only have a basic grasp of programming at the moment. Is the main function ever truly exited, and reentered?
[/quote]
#include <ctime>
#include <cstdlib>
// All your other includes and stuff...


int main()
{
std::srand(std::time(NULL));

// Now you continue with your code, never calling std::srand again (unless you really know what you're doing)
}

Share this post


Link to post
Share on other sites

Is the main function ever truly exited, and reentered?

Yes, [font="Courier New"]main()[/font] is entered just once at the beginning of your program after all objects of static storage duration (globals) have been constructed, and exits at the end of your program just prior to destroying all objects of static storage duration.

It is forbidden by the standard to invoke [font="Courier New"]main()[/font] in any other way. it is a very special function in that respect.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!