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?