Sign in to follow this  

problems with code and a few question

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

first, what is wrong with this code ?
#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; //weapon choice 1
int ch2; //weapon choice 2
int ch3; //weapon choice 3
int heal;  //heal the character
int choice; //turn choice
int att = 1; //Player attacks
int hel; //heal


//Declare function
void ebattle();  //Enemy battle function
void battle();  //Player battle function

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;

  battle();
  ebattle();
  goto battle();  // LINE 47

  void battle()
  {                 //LINE 50
  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;
  }
  void ebattle() {    LINE 62
  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;
  }
  return 0;
}
I get the following errors: parse error before '(' (line 47) parse error before '{' (line 50) parse error beofre '{' (line 62) parse error before 'return' also, how do I end my giant if statement? I am composing the game of three giant if statemants basically(I only have on so far) so, how do I end them.

Share this post


Link to post
Share on other sites

#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; //weapon choice 1
int ch2; //weapon choice 2
int ch3; //weapon choice 3
int heal; //heal the character
int choice; //turn choice
int att = 1; //Player attacks
int hel; //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;
}
}

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;
}

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();}

return 0;
}




You can't include functions in other functions(like main). goto is evil, don't use it.

cout << " " << endl;
This is pointless. Just use:
cout << endl;

Share this post


Link to post
Share on other sites
you can't declare a function inside another function. take the void battle() function out of main, and put it just before the declaration of main. then get rid of the word goto, and it should be fine.

-me

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Forgive me if I'm wrong, but you haven't closed your main function before declaring the battle function. In fact I don't think you have closed any of the functions. Try putting an extra curly bracket } at the end of each function.

Share this post


Link to post
Share on other sites
goto battle(); // LINE 47

What is this line meant to do? This is the source of your problem. You probably want to simply finish execution, right?

In C++, opening braces must match closing braces.

if (condition) // if begins here
{
/// code
} // if ends here

void function() // function begins here
{
// code
} // function ends here

You may also use the return statement to exit a function early, and the else, break and continue keywords etc for branching.

Share this post


Link to post
Share on other sites
Well, let's see. First of all, goto's are generally considered to be bad programming practice and things to be avoided. However, since you aren't actually using goto's, I'm not going to explain that. Now, where you do have goto's - well, let me put this by example: on line 47, where you have goto battle();, change it to simply battle();. Now, take the battle function out of main() - as a separate function, it needs to be after main's }. Same with the ebattle function. Your if blocks are already ended - they go from the if(...){ to the closing }. Now, for other problems... you're comparing weapon to ch1 when you ask for weapon choice, ch1 being a variable that never got a value. What do you expect that to do? Why not compare weapon to 1, the option you gave the user to select axe? Again with the menu on the player's turn, do if(choice==1), not if(choice==att). Also, if you've learned about switch/case yet, you might consider using that for your menus. If you haven't, don't worry about it, it'll make things easier but isn't necessary. Fix all that and your program will, to some point, work just like you expected it to. Keep in mind that you'll have to write code for the other two menu options (sword and staff) before those will do anything, though. Finally, this will only go once. The player will have a turn, the enemy will attack, the player will have another turn, and then the program will quit. Try replacing your calls to battle and ebattle with code like this:

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

That way, the turns will continue until one of the players loses.

If you make those changes, you should have a simple functional game (you might need to make sure you have all your braces correct, don't want { without a } to go with it). Good luck as you continue learning, don't give up. ;)

Share this post


Link to post
Share on other sites
ok, thanks everyone. I was using the goto to restart the battle phase. oh, and I wont give up. Ive done that severla times while learning c++. it wont happen again :)

(my rating keeps going down. I wonder why? I am probably starting to become annoying.........)

Share this post


Link to post
Share on other sites
i've Finished your simple game project.

i've added some new function from stdlib.h it called randomize()

it very usefull for hit probability

i use C language borland c 3.1 because i dont have visual c++ in my laptop.

i'll hope this will help u to learn more about game programing..




#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

//----------------------------------- U Can change this setting by change its values
#define SWORDHIT 20
#define AXEHIT 30
#define STAFFHIT 40
#define ENEMYHIT 20
//-----------------------------------

//Declare global variables
int health = 100; //Player health
//----------------------new
int MaxHit=0;
int EnemyHit=ENEMYHIT;
//----------------------

int enemyh = 100; //Enemy health
int choice; //turn choice

//Declare function
void ebattle(); //Enemy battle function
void battle(); //Player battle function

int main()
{
int weapon; //Current weapon
randomize();
printf("Armor core arenas\n");
printf("Welcome to armor core arenas! The game where you fight against your\n");
printf("opponent in an arena with a weapon of your choice.\n\n");
printf("Please choose your weapon\n");
printf("1. axe\n");
printf("2. sword\n");
printf("3. staff(magic)\n\n");
while(weapon<49||weapon>51)
{
weapon=getch();
}

weapon-=48;

switch(weapon)
{
case 1:
printf("You have chosen the axe. The battle will now comence!\n");
printf("your opponet will be the crusher. he is a large opponet who also weilds an axe\n");
MaxHit=AXEHIT;
break;

case 2:
printf("You have chosen the sword. The battle will now comence!\n");
printf("your opponet will be the crusher. he is a large opponet who also weilds an axe\n");
MaxHit=SWORDHIT;
break;

case 3:
printf("You have chosen the magic staff. The battle will now comence!\n");
printf("your opponet will be the crusher. he is a large opponet who also weilds an axe\n");
MaxHit=STAFFHIT;
break;

}
printf("\n");
int turn=0;
while(health>0 && enemyh>0)
{
printf("-----------------------Turn %d --------------------------\n",++turn);
battle();
ebattle();
printf("\n\n");

}

if(health>0) printf("You WIN!!\n");
else if(enemyh>0)printf("You Lose!!\n");
else printf("Draw!!");

return 0;
}

void battle()
{
printf("It is your turn. what do you do?(1 to attack and 2 to heal)\n\n");

while(choice<49||choice>50)
{
choice=getch();
}

choice-=48;

if(choice==1)
{
int hit=random(MaxHit);

if (hit>0)
{
printf("You have attacked him! %d point\n",hit);

enemyh = enemyh - hit;
if(enemyh>0) printf("He now has %d health left\n\n",enemyh);
else printf("He now has died \n\n");

printf("Your Hit Level : ");

if(hit>(MaxHit-(MaxHit*10/100))) printf("Perfect hit !!!!!!!\n");
else if(hit>(MaxHit-(MaxHit*50/100)))
{
switch(random(3)+1)
{
case 1:
printf("Powerfull Hit !!\n");
break;

case 2:
printf("Superb Hit !!\n");
break;

case 3:
printf("Revenge Hit !!\n");
break;
}
}
else printf("Normal hit\n");

printf("\n");

}
else{printf("Missed !!\n");}
}
else
{
health = health + 20;
printf("You have healed your-self! health up to %d\n\n",health);
}

}

void ebattle() {
int hit=random(EnemyHit);
health = health - hit;
printf("It is his turn to attack! he has struck you with his mighty weapon!\n");
if(health>0) printf("you now have only %d health left!\n\n",health);
else printf("You Has Died !!");

}



[Edited by - gohacker82 on August 17, 2004 3:25:58 AM]

Share this post


Link to post
Share on other sites

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