# more problems with code(still need help!)

## Recommended Posts

ok, I tried to add the two other weapons to my game, but I got similar errors agin.
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;

//Declare global variables
int health = 100;  //Player health
int enemyh = 100;  //Enemy health
int weapon;  //Current weapon
int ch1 = 1; //weapon choice 1
int ch2 = 2; //weapon choice 2
int ch3 = 3; //weapon choice 3
int heal;  //heal the character
int choice; //turn choice
int att = 1; //Player attacks
int hel = 2; //heal

void battle() {
cout<<"It is your turn. what do you do?(1 to attack and 2 to heal)"<< endl;
cin>> choice;
if (choice == att) {
cout<<"You have attacked him!"<< endl;
enemyh = enemyh - 10;
cout<<"He now has "<< enemyh<<" health left"<< endl;
}
if (choice == hel) {
cout<<"You have healed your-self!"<< endl;
health = health + 20;
cout<<"You now have "<< health<<" health left"<< endl;
}
}

void ebattle() {
cout<<"It is his turn to attack! he has struck you with his mighty weapon!"<< endl;
health = health - 12;
cout<<"you now have only "<< health<< " health left!"<<endl;
}

void battle2() {
cout<<"It is your turn. what do you do?(1 to attack and 2 to heal)"<< endl;
cin>> choice;
if (choice == att) {
cout<<"You have attacked him!"<< endl;
enemyh = enemyh - 12;
cout<<"He now has "<< enemyh<<" health left"<< endl;
}
if (choice == hel) {
cout<<"You have healed your-self!"<< endl;
health = health + 15;
cout<<"You now have "<< health<<" health left"<< endl;
}

void battle3() {  //LINE 56
cout<<"It is your turn. what do you do?(1 to attack and 2 to heal)"<< endl;
cin>> choice;
if (choice == att) {
cout<<"You have attacked him!"<< endl;
enemyh = enemyh - 8;
cout<<"He now has "<< enemyh<<" health left"<< endl;
}
if (choice == hel) {
cout<<"You have healed your-self!"<< endl;
health = health + 30;
cout<<"You now have "<< health<<" health left"<< endl;
}

void ebattle2() {  //LINE 70
cout<<"It is his turn to attack! he has struck you with his mighty weapon!"<< endl;
health = health - 10;
cout<<"you now have only "<< health<< " health left!"<<endl;
}

void ebattle3() {
cout<<"It is his turn to attack! he has struck you with his mighty weapon!"<< endl;
health = health - 12;
cout<<"you now have only "<< health<< " health left!"<<endl;
}

int main(int argc, char *argv[])
{
cout<<"Armor core arenas"<< endl;
cout<< endl;
cout<<"Welcome to armor core arenas! The game where you fight against your"<< endl;
cout<<"opponent in an arena with a weapon of your choice."<< endl;
cout<<endl;
cout<< endl;
cout<< endl;
cout<<"1.  axe"<< endl;
cout<<"2.  sword"<< endl;
cout<<"3.  staff(magic)"<< endl;
cin>> weapon;
if (weapon == ch1) {
cout<<"You have chosen the axe. The battle will now comence!"<< endl;}
cout<<"your opponet will be the crusher. he is a large opponet who also weilds an axe"<< endl;
cout<< endl;

while(health>0){
battle();
ebattle();}

if (weapon == ch2) {
cout<<"You have chosen the sword. a mighty weapon that can quickly slice through opponets. The battle will now commence!"<< endl;
cout<<"Your opponet will be the hunter. A swift enemy who uses his bow and arrow to a great advantage"<< endl;
cout<< endl;

while(health>0){
battle2();
ebattle2();}  //LINE 112

if (choice == ch3) {
cout<<"You have chosen the magic staff. a mystical item that has extrodinary powers. The battle will now commence!"<< endl;
cout<<"Your opponet is the wizard ar naria. a powerful wizard with unknown powers."<< endl;
cout<< endl;

while(health>0){
battle3();
ebattle3();}  //LINE 120

return 0;
}


I get the following errors: implicit declaration of function 'int ebattle2(...)' implicit declaration of function 'int battle3(...)' parse error before { (line 56) parse error before { (line 70) parse error at end of input (last line) so, whats wrong? you know, I think I would be better at this stuff if I actually understood what the errors meant. I know some, but very little. is there an easier way to understand this stuff? [Edited by - jakpandora on August 17, 2004 8:27:30 AM]

##### Share on other sites
u are missing some brackets..

When you create a function ALWAYS create both brackets before proceding.

At the end of ur if statement in both battle2 and battle3 you are missing brackets.... Like I said before get a C++ book it will help you lots.

##### Share on other sites
Quote:
 Original post by jakpandoraimplicit declaration of function 'int ebattle2(...)'implicit declaration of function 'int battle3(...)'

Implicit declaration of a function, means that you're calling a function that you haven't declared. In the first case, you don't have an ebattle2 function, but you do have a battle2 function. May I suggest putting a little more thought into function (and variable) names?
EDIT: Ok, you do have an ebattle2 function.. Didn't see it, but that just illustrates my point about the function names... So the problem is the same as in the second case.
In the second case, it's because of the parse error. It's hard to see because of the lack of indentation, but it seems that you've forgotten the closing '}' for battle2().
Quote:
 parse error before { (line 56)parse error before { (line 70)parse error at end of input (last line)

The two first ones seem to be because you haven't ended your functions with '}'
Fixing those should fix the last one too.

##### Share on other sites
I compiled your code and it seems that you have local declaration functions ,try fix that .
You can get the significance of errors by looking for them in
msdn

##### Share on other sites
actually, I do have a c++ book, its just *really* bad. I cant afford to get a mew one at the moment, but when I do, im leanring everything all over again. thanks.

##### Share on other sites
I am still having problems. I fixed the code, and now I have one eror:

parse error at end of input

it is on the last line, the one with the closing curly brace. any help

##### Share on other sites
jakpandora,

May I suggest you use Thinking in C++? It's a free book, which you can either work through online, or offline (scroll down the page a little for a download site).

I don't know what book you're reading, but your code is very, very messy (I don't mean that to offend - we all have to start somewhere).

What the others have said about indentation and naming conventions, will certainly help you a lot. Even if you don't know what an error means, it's much easier to track it down if your code is readable. Don't be afraid to use white-space in order to make things easier to read. In fact, I'd strongly advise that you do make use of white-space.

Here's an example from your code:

void battle() {                 cout<<"It is your turn. what do you do?(1 to attack and 2 to heal)"<< endl;  cin>> choice;  if (choice == att) {  cout<<"You have attacked him!"<< endl;  enemyh = enemyh - 10;  cout<<"He now has "<< enemyh<<" health left"<< endl;  }  if (choice == hel) {  cout<<"You have healed your-self!"<< endl;  health = health + 20;  cout<<"You now have "<< health<<" health left"<< endl;  }}

This would be much easier to read, and hence maintain, if you used some indentation:

void battle() {               	cout << "It is your turn. what do you do?(1 to attack and 2 to heal)" << endl;	cin >> choice;	if (choice == att) 	{		cout << "You have attacked him!" << endl;		enemyh = enemyh - 10;		cout << "He now has " << enemyh << " health left" << endl;	}	if (choice == hel) 	{		cout << "You have healed your-self!" << endl;		health = health + 20;		cout << "You now have " << health << " health left" << endl;	}}

You only need to press the TAB key once, in order to get a nice indent. Some people prefer using spaces, but that's personal preference. I like using TAB, being as you only need to press it once, and not three or four times for the same effect. Also, note the extra spacing I've added, between statements such as cin >> choice;. Again, it makes things much easier to read.

Strongly consider looking at Thinking in C++. Bad habits are harder to "unlearn", if there is such a word, than they are to prevent in the first place.

-hellz

##### Share on other sites
what compiler are you using?

##### Share on other sites
I am using dev c++ 4. anybody know why this has happened? I have seen it before, and it usually goes away when I fix my code. oh, and ill check out that book. hopefully it will help.

##### Share on other sites
"parse error at end of input" is probably because of unbalanced {}..
You seem to have lots of them. Like this:
  if (weapon == ch2) {  cout<<"You have chosen the sword. a mighty weapon that can quickly slice through opponets. The battle will now commence!"<< endl;  cout<<"Your opponet will be the hunter. A swift enemy who uses his bow and arrow to a great advantage"<< endl;  cout<< endl;

##### Share on other sites
Just a thought, you might want to make your code a bit easier to use. I see you try and set up a while loop for each different weapon. If you let the user choose from the beinging and set the weapon once, all you need is one game loop (while statment). Also you can reuse your battle function without making and ebattle(), battle2(), etc. Try something like this...

#include <stdio.h>#include <iostream>#include <fstream>#include <string>#include <cstdlib>#include <cmath>using namespace std;//Declare global variablesint health	= 100;  //Player healthint enemyh	= 100;  //Enemy healthint weapon;			//Current weaponint ch1		= 1;	//weapon choice 1int ch2		= 2;	//weapon choice 2int ch3		= 3;	//weapon choice 3int heal;			//heal the characterint choice;			//turn choiceint att		= 1;	//Player attacksint hel		= 2;	//healint weaponAttack = 0;  //Bonus from weaponint healBonus	 = 0;  //Bonus from weaponint enemeyAttack = 0;  //Bonus from weapon(enemey)bool loop = true;//Prototypesint attackPoints();int healPoints();//Player battle scriptvoid playerBattle() {               	cout<<"It is your turn. what do you do?(1 to attack and 2 to heal)"<< endl;	cin>> choice;		if (choice == att) 	{		cout << "You have attacked him!" << endl;		enemyh -= attackPoints() + weaponAttack;		if(enemyh <= 0)		{ cout << endl << "You have slain your enimies!" << endl; loop=false;}		cout << "He now has " << enemyh << " health left" << endl;	}else if (choice == hel){		cout << "You have healed your-self!" << endl;		health += healPoints() + healBonus;		cout <<"You now have "<< health<<" health left"<< endl;	}}//Enemey battle scriptvoid enemeyBattle() {   	cout << "It is his turn to attack! He has struck you with his mighty weapon!" << endl;	health -= attackPoints() + enemeyAttack;	if( health <= 0)	{ cout << endl << "You have been defeated!" << endl; loop=false;}	cout<<" You now have only "<< health<< " health left!" << endl;}//Random attack numberint attackPoints(){	return int(8.0 * rand()/(RAND_MAX+1.0));	}//Random heal amountint healPoints(){	return int(15.0 * rand()/(RAND_MAX+1.0));}int main(int argc, char *argv[]){	cout << "Armor core arenas" << endl;	cout << endl;	cout << "Welcome to armor core arenas! The game where you fight against your"<< endl;	cout << "opponent in an arena with a weapon of your choice."<< endl;	cout << endl;	cout << endl;	cout << endl;	cout << "Please choose your weapon"<< endl;	cout << "1.  axe(+2 to attack, +5 to heal)"<< endl;	cout << "2.  sword(+4 to attack, +0 to heal)"<< endl;	cout << "3.  staff(magic)(+0 to attack, +15 to heal)"<< endl;	cin  >> weapon;	if (weapon == ch1) 	{		weaponAttack = 2;		healBonus	 = 5;		enemeyAttack = 2;		cout << "You have chosen the axe. The battle will now comence!"<< endl;		cout << "your opponet will be the crusher. he is a large opponet who also weilds an axe"<< endl;		cout << endl;	} else if(weapon == ch2){		weaponAttack = 4;		healBonus	 = 0;		enemeyAttack = 0;		cout << "You have chosen the sword. a mighty weapon that can quickly slice through opponets. The battle will now commence!"<< endl;		cout << "Your opponet will be the hunter. A swift enemy who uses his bow and arrow to a great advantage"<< endl;		cout << endl;	} else if(weapon == ch3){		weaponAttack = 0;		healBonus	 = 15;		enemeyAttack = 2;		cout << "You have chosen the magic staff. a mystical item that has extrodinary powers. The battle will now commence!"<< endl;		cout << "Your opponet is the wizard ar naria. a powerful wizard with unknown powers."<< endl;		cout << endl;	} else {		cout << "You have selected an invalid item" <<endl;	}	while(loop)	{		if(health > 0)			playerBattle();						if(enemyh > 0)			enemeyBattle();	}	return 0;}

it is a bit more organized and less repeted code. I threw in some random number for attacks and used your base number to get the weapon and heal bonus. Nothing perfect, but at least a start in the right direction.

dimebag

##### Share on other sites
ok, thanks for everyones help. I dont understnad what you mean by unbalance {}, but I am just going to use parts of the code the annonymous poster supplied, to get myslef on the right track.

##### Share on other sites
Unbalanced {}: Brackets (and parentheses etc.) need to be balanced. This means that for every { there should be a }.
So if you have
if (weapon == ch2) {

you need to have an ending }..
Brackets are used to mark blocks, { marks the beginning and } marks the end.

##### Share on other sites
Sorry for the Anonymous post, forgot username and pass.

I threw that together, you could probably split the weapons into a switch statement, and make the player, enemey a little more robust, but I figured ti would give you an idea of how reuseablity is your friend. Why have 3 different functions that do the same thing with only one variable diference? Instead combine the functions, and change the variable. If you have any more questions, feel free to e-mail me. villagetech@sterlinghousing.com.

dimebag

##### Share on other sites
Try to use #define

int wp1 = 1; // this is not effective!!

try this

#define wp1 1

it's the same like : int wp1 = 1; //but more efficient..

and try to give a tab space if you make a new block (scope)

example:

for (int i=0;i<10;i++){        if(i==9)        {                cout<<"Finish"<<endl;        }        else        {                cout<<"item "<< i << endl;        }}[\source]it's easy to read..

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627665
• Total Posts
2978529

• 10
• 10
• 12
• 22
• 13