Local Function Definitions are illegal...very evil problem

This is a discussion on Local Function Definitions are illegal...very evil problem within the C++ Programming forums, part of the General Programming Boards category; Hi. This code is a portion of a whole program, and I get the error message stated in the thread ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    18

    Question Local Function Definitions are illegal...very evil problem

    Hi. This code is a portion of a whole program, and I get the error message stated in the thread title...but I can't seem to fix it, no matter what I do.

    Here is the Code:

    Code:
    char User_Choice;
     void UserThree(char User_Choice){
    	 std::cout << "Do you like the number 3? (Yes=Y, No=N)\n";
    		std::cin >> User_Choice;
    		if {(User_Choice == y)
    			std::cout << "You are cool\n";
    		}
    		else {
    			if  (User_Choice == n)
    				std::cout << "You are Un-cool\n";
    		}
    		else {
    			if (User_Choice != y, n)
    				std::cout << "Invalid Decision" << endl;
    		}
    
    
     }
    Can someone out there point out what I should do?

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Are you trying to define your function inside a function or inside main? That's a nono.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    Ahh...this function is inside the main() function...should I move it out?

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Yes, you cannot have a function definition within another.

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    yes -- move it out

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    first put ' around your characters
    Code:
    if  (User_Choice == 'n')
    don't use else { if, use else if
    Code:
    else if  (User_Choice == 'n')
    thirdly use && (AND) to make sure it's not one thing and the other.
    Code:
    else if (User_Choice != 'y' && User_Choice != 'n')
                    std::cout << "Invalid Decision" << endl;
    or you could just use else
    Code:
    else
     std::cout << "Invalid Decision" << endl;

    edit also use endl instead of \n
    Code:
    std::cout << "Do you like the number 3? (Yes=Y, No=N)" << endl;
    Code:
    void UserThree(char User_Choice){
            char User_Choice;
            std::cout << "Do you like the number 3? (Yes=Y, No=N)" << endl;
            std::cin >> User_Choice;
    
            if (User_Choice == 'y') {
                std::cout << "You are cool\n";
            } else 
            if  (User_Choice == 'n') {
                    std::cout << "You are Un-cool\n";
            } else {
                    std::cout << "Invalid Decision" << endl;
            }
    }
    Last edited by Brian; 02-19-2005 at 06:33 PM.

  7. #7
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Yep. Move it outside main. Then you can call it from inside main. If the function is defined after main, then be sure to use a prototype before main (or the first use of the function call).

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    When I move it outside of main() I get all kinds of errors, though.

  9. #9
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    post some of them please

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    Nevermind...It's working now, thanks all.

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    Actually, I am having a typedef error..."warning C4091: 'typedef ' : ignored on left of 'my_soundtrack' when no variable is declared"

  12. #12
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    My guess is that you're using typedef somewhere incorrectly. Can't say until you post more code.

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    I will post it later...after trying to use this new advice I messed up the whole file...

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    Try writing less code between pressing "compile", then you have a much better idea of what caused the new set of error messages to appear.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-23-2008, 06:21 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. 'Local Function Definitions are illegal' ?
    By kinghajj in forum C++ Programming
    Replies: 7
    Last Post: 11-10-2003, 08:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21