Hi guys, a bit of a lengthy post, but bear with me please:
I've had a small problem in my program where I declared functions for a simple menu.
There are several menus that are embedded - that is, one can lead to the other and back again.
To do this, i've made each separate menu it's own function. So, I declare my functions before the main (all in one .cpp) and one by one they work. What doesn't work is when one function calls the other in it's own code, like this: (short example)
I've left out a bit of code obviously, but basically, I get a compiler error, because the function showMenuOne(), can't see showMenuTwo() and therefore can't execute the instruction within itself.Code:void showMenuOne() { if(selection == 1) { showMenuTwo(); } //1. go to menu two //2. exit } void showMenuTwo() { if(selection == 2) { showMenuOne(); } //1. go to menu one //2. exit } int main() { /// }
I played around with this problem for a little while, and came up with the solution of declaring the functions further up ahead the file, like this:
This works both during compile and run-time. So basically, my question is, was this the correct way of making functions "see" each other, or did I just "hack" my way through? If I did "hack" my way through, what would be the correct approach?Code:void showMenuOne(); void showMenuTwo(); void showMenuOne() { //code as before } void showMenuTwo() { //code as before }
Thanks,
-K



LinkBack URL
About LinkBacks



You shouldn't have to resort to recursion like that just to get a menu to display. You could, for example, use something like this: