-
Functions
Hi, I'm used to using GOTO's alot for all my problems but now I need to learn the wonderful world of functions. I can't figure this out, though:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
int attackmiss = (rand() % 100) + 1;
char x;
char y;
int yourhealth = 100;
int enemyhealth = 100;
char yourname[15];
void mainmenu (void)
{
system("CLS");
cout<<"What whould you like to do?\n\n";
cout<<"(f) - Fight\n"
<<"(w) - Buy new weapon\n"
<<"(a) - Buy new armor\n"
<<"(p) - Buy new potion\n"
<<"(s) - Look at your current stats\n\n";
cin>>x;
switch(x)
{
case 'f':
{
void battle (void) //This is where I get the error becasuse the battle function definition is inside the mainmenu function definition
{
system("CLS");
cout<<yourname<<"\t\t\t\t\t\t\tEnemy\n";
cout<<"Health: "<<yourhealth<<"%\t\t\t\t\t\tHealth: "<<enemyhealth<<"%";
cout<<"\n\n(a) - Attack";
cout<<"\n(p) - Use a potion";
cout<<"\n(r) - Run away\n";}
cin>>x;
if (x == 'a')
{
cout<<"\n\nAttack with what?\n\n";
cout<<"(h) - Your hand\n"
<<"(c) - Crossbow\n"
<<"(a) - Axe\n"
<<"(b) - Bronze sword\n"
<<"(s) - Silver sword\n"
<<"(g) - Gold sword\n"
<<"(p) - Platinum sword\n"
<<"(f) - Flamethrower\n"
<<"(r) - Rocket launcher\n";
cin>>y;}
if ((y == 'h') && (attackmiss>=75))
{cout<<"You miss";
cin>>y;
battle();}
else
{cout<<"You hit him! Good job";
cin>>y;
battle();}
}
if (x == 'p')
{
cout<<"\n\nUse what potion?\n\n";
cout<<"(f) - Fire blaze\n"
<<"(t) - Electric thunder\n"
<<"(w) - Water Tsunime\n";
cin>>y;
}
if (x == 'r')
{mainmenu();}
break;
}
}
void entername (void)
{
system("CLS");
cout<<"Enter your name: ";
cin>>yourname;
mainmenu();
}
int main()
{
cout<<"\t\t\t\tXtreme Fight"
<<endl<<endl<<endl
<<"(s)tart game"
<<endl<<endl
<<"(e)xit"
<<endl<<endl;
cin>>x;
if (x == 's')
{entername ();}
return 0;
}
When I run this, i get the following error:
C:\Program Files\Microsoft Visual
Studio\MyProjects\XtremeFight\XtremeFight.cpp(56) : error C2601: 'battle' : local function definitions are illegal
I know I can't define a function inside a function, so in this case, how would I solve this problem?
-
Code:
void mainmenu (void)
{
system("CLS");
cout<<"What whould you like to do?\n\n";
cout<<"(f) - Fight\n"
<<"(w) - Buy new weapon\n"
<<"(a) - Buy new armor\n"
<<"(p) - Buy new potion\n"
<<"(s) - Look at your current stats\n\n";
cin>>x;
switch(x)
{
case 'f':
{
Where does this function end?
Code:
void mainmenu (void)
{
system("CLS");
cout<<"What whould you like to do?\n\n";
cout<<"(f) - Fight\n"
<<"(w) - Buy new weapon\n"
<<"(a) - Buy new armor\n"
<<"(p) - Buy new potion\n"
<<"(s) - Look at your current stats\n\n";
cin>>x;
switch(x)
{
case 'f':
// Do something
} // End switch
} //end function
-
Code:
// Pre-define the function first
void battle();
void mainmenu()
{
...
}
// Now define battle()
battle()
{
...
}
Jusr pre-define the battle() function. It's illegal to declare a function insider another, as you saw. Try that.
Brendan
-
Yeah, but...
The reason why it seems like the battle function starts in a strange place (in the middle of mainmenu function) is because I needed something to go that particular part. My problem is that I repeat pretty much all of my code so it makes it difficult when I place a function that has an actual beginning and ending; it all seems to meld in together. Like in this instance i can't end the mainmenu function before the battle function starts becuase the mainmenu function is more code than that....do you understand what i mean?
Also, if this doesn't work out, is there anything else that I can use instead of GOTO statments besides functions?
-
As your basic structure for the program, do sometihng like this:
Code:
#include <iostream>
// other include files
// declare your globals (the less the better!)
// below your are defining the structures of your functions
// this means that you can call a function within another funciton with ease
void MainMenu(void);
void Battle(void);
void EnterName(void);
// and now your main function
int main(void){
cout<<"\t\t\t\tXtreme Fight"
<<endl<<endl<<endl
<<"(s)tart game"
<<endl<<endl
<<"(e)xit"
<<endl<<endl;
cin>>x;
if (x == 's'){
EnterName ();
}
return 0;
} // end of main
//now you put your functions here
void MainMenu(void){
system("CLS");
cout<<"What whould you like to do?\n\n";
cout<<"(f) - Fight\n"
<<"(w) - Buy new weapon\n"
<<"(a) - Buy new armor\n"
<<"(p) - Buy new potion\n"
<<"(s) - Look at your current stats\n\n";
cin>>x;
switch(x)
{
case 'f':
{
Battle(); // run your battle function
}
case 'w':
{
BuyWeapon(); // your buy weapon function
// you wil need to create this function as you do the others
}
} //end of MainMenu
void Battle(void){
// insert your battle function
}
void EnterName(void){
// insert your enter name function
}
thats how it should go basically. Hopefully that makes some sense. ;)
-
A few notes...
First, if you need to get from the moddile of one function to another function, define it elsehwere, and call it Battle() from within mainmenu().
Another thing... When you need to get BACK to where you were before, use returns. You program is very recursive, and that will get you in trouble if you run it too long.