Thread: illegal break

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    34

    illegal break

    I'm trying to shorten a messy and uber long tic tac toe program(not coded by me) by using functions but there's an error called illegal break


    Code:
    int Xwon()
    {
    printf("\n\t\tX won!!!\n");
    				printf("\n1. New Game 0. Quit: ");
    				scanf("%d", &choice);
    				if (choice== 0)
    				{
    					printf("\nPress Enter");
    					break;
    				}
    				else
    					return main();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are trying to break from a function? Perhaps you want to return from the function.

    What's with returning the return value of main()? It is not good practice to call main().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    return main();
    Why on earth are you calling main()?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    34
    This code wasn't coded by me.
    I'm trying to shorten it by using functions on repetitive codes.
    Last edited by Trafalgar Law; 09-25-2008 at 10:09 PM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Either way you shouldn't be calling main(). It's prohibited by the standard because the implementation is allowed to add initialization calls to the beginning of main(). (Please correct me if I am wrong. I am not entirely sure about this.)

    I'm sure it's not a good idea to call main(), though, as other people have pointed out.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    34
    nevermind this post I'll just create my own code

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Trafalgar Law View Post
    nevermind this post I'll just create my own code
    An extremely good idea when the code you're dealing with is this broken!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cyberfish View Post
    Either way you shouldn't be calling main(). It's prohibited by the standard because the implementation is allowed to add initialization calls to the beginning of main(). (Please correct me if I am wrong. I am not entirely sure about this.)
    Since this is the C forum, you are wrong. But it's still not a good thing to do, and if it's done in C++ it is indeed illegal in the standard. [main in C++ may well contain the code to construct global objects].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Since this is the C forum, you are wrong. But it's still not a good thing to do, and if it's done in C++ it is indeed illegal in the standard. [main in C++ may well contain the code to construct global objects].

    --
    Mats
    Why did they make that difference between C & C++ anyways?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The OP was asking about an illegal break.

    "break" is only permitted in code blocks from "for", "do", "while" loops, or "switch" cases. In the example above, the "break" is not within any of such blocks. The next outer level would be the function itself which is why people started to talk about breaking out of the function.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Why did they make that difference between C & C++ anyways?
    So that main can be used to implement the "global constructor" functionality in C++. Since C doesn't have global construction, there was at the time of specifying the language, no need to prevent that. And a standards committe will not change the language standard simply because some other closely related language doesn't support the functionality that your language supports - that just means an unecessary break of backwards compatibility (even if its use is ever so ugly), whcih wins you NO friends at all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    So that main can be used to implement the "global constructor" functionality in C++. Since C doesn't have global construction, there was at the time of specifying the language, no need to prevent that. And a standards committe will not change the language standard simply because some other closely related language doesn't support the functionality that your language supports - that just means an unecessary break of backwards compatibility (even if its use is ever so ugly), whcih wins you NO friends at all.

    --
    Mats
    But I mean, why can't they do the global construction before calling main()?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    But I mean, why can't they do the global construction before calling main()?
    Of course, but since the compiler may not have anywhere else that it can put those things, because the compiler has no knowledge of what happens before main. It is not stated in the standard that main should do global construction, only that global construction happens some time before the user's code is called in main, and that main is not allowed to be called from the user's code. This ALLOWS the implementor to do the global construction inside main, but it is perfectly valid to do that somewhere else too.

    gcc DOES implement main as the place to call global constructors, but I beleive MS VC++ doesn't.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Of course, but since the compiler may not have anywhere else that it can put those things, because the compiler has no knowledge of what happens before main.
    If the compiler doesn't setup what happens before main() then who does?
    I thought the compiler creates everything from the point when the OS starts the program?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Don't pick on semantics, cpjust. The compiler does not know anything. It does precisely what you tell it to do. And if you start assuming it is just going to know what you mean without being very explicit, you will probably find you just wrote a program that triggers some compiler warnings... Or your linker will not be too thrilled with your compiler's output. If you ever write a program in assembly language you will find there is no magic to beginning a program. There are no glorious steps that one needs to take prior to executing a function called "main." Your compiler doesn't work miracles, then call your main implementation. It just writes out the machine code required to execute your main fuction.

    And VC++ does do global initialization slightly differently than GCC, for the record. Microsoft initializes things prior to calling main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM