Thread: Functions

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Question 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?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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

  3. #3
    harryp
    Guest
    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

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Unhappy 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?
    Last edited by funkydude9; 09-03-2002 at 09:43 PM.

  5. #5
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    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.
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM