Sign in to follow this  

more problems with code(still need help!)

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

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<<"Please choose your weapon"<< 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by jakpandora
implicit 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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;

As been said before indentate your code better! It will help you find these errors faster.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 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

int weaponAttack = 0; //Bonus from weapon
int healBonus = 0; //Bonus from weapon
int enemeyAttack = 0; //Bonus from weapon(enemey)

bool loop = true;

//Prototypes
int attackPoints();
int healPoints();

//Player battle script
void 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 script
void 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 number
int attackPoints()
{
return int(8.0 * rand()/(RAND_MAX+1.0));
}

//Random heal amount
int 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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..

Share this post


Link to post
Share on other sites

This topic is 4865 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.

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