Sign in to follow this  
jakpandora

whats wrong with my function?

Recommended Posts

ok, yesterday, somebody said I should write my battle system with functions instead of just writing it all out. so I tried that, and it wouldnt compile. heres the code:
//Rpg practice - Experiment in writing an rpg battle system
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

void battle(); //Battle function prototype
int main(int argc, char *argv[])
{

  int health = 100; //Starting health
  int choice;
  int monhea = 100;
  int att = 1;
  cout<<"Rpg experiment"<< endl;
  cout<<"You have been attacked by a monster! What do you do?"<< endl;
  cout<< health<<" remaining"<< endl;
  cin>> choice;
  if (choice == att) {
  cout<<"You attack the monster. atack succes!"<< endl;
  monhea = monhea - 10;
  cout<<"Monster has "<< monhea<<" health left"<< endl;
  }
  battle();

  system("pause");

  return 0;
}

void battle()
{
cout<<"The monster attacks!"<< endl;
  health = health - 12;
  cout<<"you have "<< health<<" health left"<< endl;
}


oh, and the error is either "health undefined" or "health undeclared" I dont remember. oh, and I need to somehow return to a specific line of code. I wanted to use gotos, but nobody would tell me. how else would I do this?

Share this post


Link to post
Share on other sites
Health and that are local to your main function.

Two solultions.

1)Pass Health as a parameter.

2)Easiest of all. Make them global. Just move those veriable definitons outside the main function.

E.g


int health = 100; //Starting health
int choice;
int monhea = 100;
int att = 1;

void battle(); //Battle function prototype
int main(int argc, char *argv[])
{

cout<<"Rpg experiment"<< endl;
cout<<"You have been attacked by a monster! What do you do?"<< endl;
cout<< health<<" remaining"<< endl;
cin>> choice;
etc etc etc




When the Battle function returns it will be to the line it was called from.

Which line do you need it to return to?

Share this post


Link to post
Share on other sites
I need it to the line that says "you have been attacked by a monster" so it can repeat the battle phase until you or the monster dies

Share this post


Link to post
Share on other sites
Don't use gotos unless you absolutely have to (which is pretty much never with C/C++). You probably want loops. Look up for() and while() in your favourite C manual.

Share this post


Link to post
Share on other sites

//Rpg practice - Experiment in writing an rpg battle system
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

void battle(); //Battle function prototype

int health;
int monhea;

int main(int argc, char *argv[])
{

health = 100; //Starting health
monhea = 100;

int choice;

int att = 1;

cout<<"Rpg experiment"<< endl;


while ( (health > 0) && (monhea > 0) )
{

cout << "You have been attacked by a monster! What do you do? (1 to FIGHT!)"<< endl;
cout << health<<" remaining"<< endl;
cin >> choice;

if (choice == att) {
cout<<"You attack the monster. Attack success!"<< endl;
monhea = monhea - 10;
cout<<"Monster has "<< monhea<<" health left"<< endl;
}

battle();

}

cout << "Battle is over!" << endl;

if (health <= 0)
cout << "Player is dead!" << endl;
else
cout << "Monster is dead!" << endl;


system("pause");

return 0;
}

void battle()
{
cout<<"The monster attacks!"<< endl;
health = health - 12;
}







Try that...

What I've done is put your battle into a while loop which terminates if your health or the monsters health is <= 0. I've also made your health and the monster's health global variables, which are set to 100 when the program is run. The battle function is called once per loop in the main while loop.

You do know that the player can never win that battle, right?

Share this post


Link to post
Share on other sites
I would suggest going to a book store and buying a C++ Primer book. No offense or anything but its the best way to get the basic knowledge of the language. From what uve written, it appears that you are just starting to write in C++ and i wouldnt suggest to take writting a game as ur first steps into a programming language, becuase atm you dont have the knowledge you need to grasp how to structure your code correctly and all you will be doing is going back again and again rewritting everything.

Get some basic knowledge then start writting some simple programs that use what you want in ur game.. Once you feel you have a decent grasp of the language then jump into writting something of the scale of a game.

This is no way a flame, just trying to help put you on the right path.

Gluck =)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this