c++ calling main() function

i just finished a project for a c++ class that i wrote at home on my computer, compiled with gcc. when i brought the code into school it would not compile, it would complain that cannot call main() function. at school we use ancient borland c++ from 1995. anyway my program has 20 different functions, and each one hasa menu to start the program over, some with switch structure, and others with else if, everyone calls the main function when it is started over, so my question is, which is right? in c++ should you be able to call the main function from other functions? or is this a lax in conformity with gcc?

---

i simply created a new function identical to main() and called that when i need to start over.

I just tried this on several compilers. Ansi C seems to allow calling main(). Ansi C++ seems to always disallow it.

The technique that you describe is a bug. If you were using recursion where you are guaranteed to eventually pop up all the levels that is one thing. But you apparently want to call main() whenever the user feels like it. Each time that you do this, you push another frame on to the stack. Eventually you will overflow your stack. Even if your stack is so large that the program doesn't abort before the box gets rebooted, you have a memory leak.

i see. so i actually should create a seperate function that is identical to main() to allow the user to restart? anyhow, thank you very much for the responce!

That would just be more of the same. Anything with the potential to result in an unlimited depth of function calls will eventually exhaust resources or consume far too many resources.

Ideally the each function should simply return when they are done. And the main function should simply have a loop that displays the menu and calls the proper function. This is the structured way and it's the only way I program.

But lots of people disagree with me (including such people as Richard Stevens and Dennis Ritchie). So I should tell you that there is also setjmp()/longjmp(). You call setjmp at some point in your program...like just before you print the main menu. Later, even in some other function, you call longjmp. And then the next thing that happens is that you're back right after the setjmp and stack pointer is magically unwound as well. This will not leak memory.

A third technique is to re-exec the program. I think that this is pathetic and here I can claim broad support.