Returning From One Function To Main

Hi All,

I've done the bulk of my code and I'm required to do one more thing and then it's complete.

Within my functions, I want to be able to send the user back to int main() once they've finished entering the details etc...

Take a look at the code and it'll make more sense, I've included comments to make it easier.

Once again, there may be a difference in how people write syntax and I understand that, however, I am not looking for people to pick up on that, I would appreciate it if you all could tell me (or at the very least guide me) on how to instigate the above.

Code URL: http://codepad.org/GQ0PaoIp
You don't need to do anything to make this happen. It's how functions work. When they finish, the program flow goes back to where the function was called and continues.

I think you already know this. Do you mean that you want to go back to the start of main, or back to just before the menu is displayed?
Last edited on
Well, every time I run the program, I can get so far, then once I have entered the information, it closes the program... so how do I send it back to int main()?
OK, maybe you don't understand already.

When you call a function from main, that function runs. When that function finishes, what happens? The flow of execution goes back to what called that function, so it goes back to main. That's how it works. It does it all by itself. It does NOT go back to the start of main. It goes back to the place in main where the function was called, and carries on from there. Is that clear?
A function will automatically return to its calling function once it is finished. In the case of a void function it returns implicitly once you reach the end of the function scope.
You might not be realizing this, since the function returns, but then your main program also terminates because after the function call inside the switch statement there is nothing else. If you stick the switch menu inside a while loop you can have a user repeatedly choosing options.
Also, just happened to catch this while scrolling down your code, but for the 4x4 constructor you put number of seats as 4-7, which is four minus seven, and not four to seven.
@Moschops

If you can't talk to people with respect for the fact this is a "Beginners" forum, then I can safely suggest you go elsewhere.

@BlackSheep

Thank you, I understand now, as for the number of seats, I have changed it now to just 7. Thanks for pointing that out to me :)
Oh, I'm sorry. I thought this was just "Beginners", not "please be psychic and magically know in advance how much I already understand, and be ever so careful not to imply that I don't understand something, even though I've clearly come here because I don't understand".

If you can't handle people trying to gauge how much you do and do not already understand, then this is not the right place for you. If you can't handle people directly addressing your quiestion without sugar-coating it and making you feel good, this is not the right place for you. If you can act like an adult, and deal with being treated like a sensible adult, then stay.

Have I missed something? I'm failing to see the problem with Moschops' post.

I thought it was clear and helpful. I'm not sure where the inference of a lack of respect has derived from.
Last edited on
I'm not getting into an argument with a puny minded little child. Do you not understand the meaning of "Beginner"? Perhaps I can give you a link to the definition in a dictionary?

I asked a direct and specific question and then replied with what was happening and what I wanted to do... and
If you can act like an adult, and deal with being treated like a sensible adult, then stay
- in which case, perhaps questioning whether you should be here may be in order?

Just by looking at your previous answers with other people, just to mention one: http://www.cplusplus.com/forum/general/72989/

I'm not going to even read your code, because it's just like all the rest of your code; horrific.


You obviously have a problem with talking to people with any respect, if you want it yourself, consider giving it.

Here's that link to the dictionary by the way, I did promise it after all.

http://dictionary.cambridge.org/dictionary/british/beginner?q=beginner
if you want it yourself, consider giving it.


I don't want your respect.

Eagle, you mistake "direct and true" for ... well, I'm not quite sure what you think is going on.

If you want to be mollycoddled, try another forum. Here, we tend towards giving people simple, honest answers that directly address their problems.

I respect posters here; I respect their maturity, and I respect that they have better things to do that be hugged and told how wonderful they are, and that they just need answers, and I respect that they can handle the fact that they don't already understand everything. In short, I treat them like adults.

This is where my contribution to this thread ends; there are people here who do actually want answers to questions.
Last edited on
I believe this will explain a little better.
http://www.cplusplus.com/forum/beginner/1/

As in regards to moschops, he did everything most of the rest of us would do. And I believe what you meant to ask was how do I keep displaying my menu over and over again until someone wants to quit. In regards to THAT question, use a while or do while loop. As your program is now, your user will be allowed to enter one option, at most 2, before the program terminates. If you would like an example of that, I'd gladly help you understand that.

As a side note, we're not hostile people, but sometimes we will ask extra questions to ensure the person understands what they're actually asking us. Please take anything we say here with a grain of salt. We're here to help, not bash. Please keep this in mind.
Hey, I respect Moschops quite a bit (I hand out the link to his article on pointers to almost ever newcomer to the language). I actually tend to hope he comes and answers my questions. He's generally straight to the point, and gives some insight as to WHY the answer is what it is.

I've been here for almost a year now, and I have never seen someone to be rude for no reason, except for one occasion where an account was compromised.
Topic archived. No new replies allowed.