Jump to content
  • Advertisement
Sign in to follow this  
forestlaw89

I'm a newb in C++ and need some help

This topic is 4835 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

This prgram wont execute until monsters life is finished and I need help: Here's the source code: #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cstdlib> #include <ctime> using namespace std; int dam_cal_warrior(int& attack, int& defense, int& result); int main() { vector<string> mon_attacks; mon_attacks.push_back("meteor"); mon_attacks.push_back("ultima"); int warrior_health= 1000; int creature_health= 10000; string action; int war_def= 350; int mon_defense= 200; int war_attack=500; // calculations srand(time(0)); random_shuffle(mon_attacks.begin(), mon_attacks.end()); const string MON_ATT= mon_attacks[0]; int dead= 0; int mon_dead= 0; int war_damage; int war_result; cout<<"Welcome to the rpg damage test. This is not a real game but a sample\n"; cout<<"engine for damage calculation. Just trying it out for sample\n"; cout<<"\nYou are a warrior in this situation and you only have the command\n"; cout<<"'attack'. When you attack this creature the battle will begin\n"; cout<<"Type attack: "; cin>> action; while( creature_health != mon_dead){//here's where the problem is dam_cal_warrior( war_attack, mon_defense, war_result); creature_health= creature_health-dam_cal_warrior( war_attack, mon_defense, war_result); cout<< "You attacked and did "<<dam_cal_warrior( war_attack, mon_defense, war_result)<<"\n"; cout<<"damage to the monster, it has "<<creature_health<<" left."<<endl; } cout<<"YOU WIN"; cin.get(); cin.get(); } int dam_cal_warrior(int& attack, int& defense, int& result) { result= attack- defense; return result; }

Share this post


Link to post
Share on other sites
Advertisement
First off, for future reference, try to put your code in a source window(type [ source ] CODE HERE [ /source ] without the spaces between the [].

while( creature_health != mon_dead){//here's where the problem is
dam_cal_warrior( war_attack, mon_defense, war_result);
creature_health= creature_health-dam_cal_warrior( war_attack, mon_defense, war_result);
cout<< "You attacked and did "<<dam_cal_warrior( war_attack, mon_defense, war_result)<<"\n";
cout<<"damage to the monster, it has "<<creature_health<<" left."<<endl;
}


The problem is that you aren't requesting any new input in this block code so it's gonna keep going until the monster is dead. I can't really help you out not knowing the exact behavior you Want to get though. Basically what you're saying now is "until the health of my monster is zero keep attacking"

Also, you don't really need to != mon_dead part, unless you think you'll change the concept of "dead" to mean "less than zero health" or something. You could do:
while(creature_health > 0)

or just:
while(creature_health)


You'd probably need to try this code:
do {
cin>> action;
dam_cal_warrior( war_attack, mon_defense, war_result);
creature_health= creature_health-dam_cal_warrior( war_attack, mon_defense, war_result);
cout<< "You attacked and did "<<dam_cal_warrior( war_attack, mon_defense, war_result)<<"\n";
cout<<"damage to the monster, it has "<<creature_health<<" left."<<endl;
} while(creature_health);


This will run at least once, and continue until you're dead. You still need to take some action based on the action though. Hope this helps.

Share this post


Link to post
Share on other sites
hi!

there are a few things i would change.

1. change 'int dam_cal_warrior(int& attack, int& defense, int& result)' to
'int dam_cal_warrior(int attack, int defense)'

don't call all those values by reference so they can be changed within the method. if you got a return value you don't need the result in the function-call ...do you ???

2. don't check for (creature_health != mon_dead) that's very unlucky to happy except your hero can only do 1hp damage per hit...if he can do more then 1hp per hit you might 'miss the death' of the monster..
hp before hit 3 and after -1 ...and the loop goes on.

hope this was of any help to you

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!