Help with Recursive on all Paths Warning

Ok, I am a college student, and have been working on a Character Sheet program. Right now all I am trying to accomplish is to create a class for the main menu, the class consists of two parts right now, and the second one is where I am getting this warning with. It causes the program to freeze and close, I was wondering if there is another way to program this part, all it should do is act like a normal int to allow menu selection, and I don't know if I am going about this the right way. I have been tearing my hear out for about a week or so with this error and have now resorted to actually posting it.

I am using Visual Studio 2012 Ultimate for this as well.


Here is my main, note that it has a lot of empty spaces since I am just trying to get the menu to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "stdafx.h"
#include "mMenu.h"
#include "cStats.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	
	mMenu mainmenu;
	mainmenu.displayMessage();
	if(mainmenu.menuSelection() == 1)
	{
	       cout << "Still working on it" << endl;
	}
	if(mainmenu.menuSelection() == 2)
	{
		cout << "Still Working on this" << endl;
	}
	if(mainmenu.menuSelection() == 3)
	{
		cout << "Still working on this" << endl;
	}
	if(mainmenu.menuSelection() == 4)
	{
		cout << "Still working on this" << endl;
	}
	if(mainmenu.menuSelection() == 5)
	{
		cout << "Still working on this" << endl;
	}
	if(mainmenu.menuSelection() == 6)
	{

	return 0;
}
};


The mMenu.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef MMENU_H
#define MMENU_H
#include <iostream>
using namespace std;
class mMenu
{
public:
	void displayMessage();
	
	int menuSelection();
	
};
#endif MMENU_H 


And finally the mMenu.cpp:

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

#include "stdafx.h"
#include "mMenu.h"
#include <iostream>
using namespace std;

void mMenu::displayMessage()
	{
		cout << "Character Sheet: Hound" << endl
			<< "Presented by Heltec" << endl
			<< "1: Character Stats" << endl
			<< "2: Character Skills" << endl
			<< "3: Character Equiptment" << endl
			<< "4: Character Bio" << endl
			<< "5: View All" << endl
			<< "0: Quit" << endl;
	}	
	int mMenu::menuSelection()
	{
		return menuSelection();
		
	}


The recursive on all paths is caused by the:

1
2
3
4
5
6
7

int mMenu::menuSelection()
	{
		return menuSelection();
		
	}


I just thought it would be helpful to see the program as a whole.

Any advice will help, I look forward to any and all help I can get! Thanks in advance.
Last edited on
1
2
3
4
5
int mMenu::menuSelection()
	{
		return menuSelection();
		
	}


So, you call the function. The body of the function is entered, it calls itself. The body of the function is entered, it calls itself. The body of the function is entered, it calls itself.

I'm having a hard time understanding how you can't see what's wrong with that, especially since your compiler was kind enough to point it out for you.
Your menuSelection function has no base case, how did you expect it to stop recursing? The compiler does not know what magic is ;)
If I take out return menuSelection(); it kindly tells me it must return a value:

Error 1 error C2561: 'mMenu::menuSelection' : function must return a value c:\users\yhivu\documents\~school\int. programming\final project\character sheet\character sheet\mmenu.cpp 19 1 Character Sheet


So, as you see I do not know what to put here, I was just seeing if I could get some advice as to what to put here.
What you originally had was "This function must return what it returns", which worked, until it used up all your memory and crashed. Then you changed it to "This function must return something but I won't tell you what", and the compiler flat out could not guess what you wanted it to do.

Here is an example of a function that returns a value:
1
2
3
4
5
6
7
int Choice()
{
    std::cout << "Enter a number: ";
    int x = 0;
    std::cin >> x;
    return x;
}
Last edited on
I understand the example of the function returning the value, but I can't make my class value = 0. So, do I need multiple values in my class to ask for a simple integer so it doesn't keep returning itself to itself?

Like:
1
2
3
4
int mMenu::menuSelection()
{
            return selection();
}


If so, how do I implement this into the main file?
I think I would begin by describing what menuSelection is supposed to do. What value is it supposed to be returning?

If you had:

1
2
3
4
5
int main()
{
   mMenu menu ;
    int x = menu.menuSelection() ;
}


describe what value x is holding after the call.
Yhivu wrote:
but I can't make my class value = 0
Did you miss line 5? The 0 is overwritten when the suer enters valid input, but if the suer enters e.g. "hello" that would fail and x would keep its value of 0. If not assigned to it would have random memory, which is bad.
Well, to put it simply a value 1-6, i could easily take this out of the class, and have a normal int allow for the menu selection, I was just hoping to have my main at a minimum, allowing the classes to do all of the work. But at this point I don't understand how I am going to allow this implement to work out.

Sorry, I didn't see your last post, I did try that but it still has the recursion warning and crashes. Even with the 0 value.
Last edited on
None of the code I posted involves the main function.
Well, to put it simply a value 1-6


And how would this value be determined? What does it represent?
The value is determined by the user, then the menu itself will be able to load the appropriate page for the user. I feel like I am missing a large chunk of code and this is feeling more complex than it should right now.
If the value should be determined by the user, what does that suggest about the implementation of menuSelection? Should you perhaps get that value from the user therein?
Well yes, but with me putting in std::cin or just cin into the mMenu.cpp file it tells me it has no operators to do so, and it still crashes if I add it to the main file.
Have another look at my example function:
1
2
3
4
5
6
7
int Choice()
{
    std::cout << "Enter a number: ";
    int x = 0;
    std::cin >> x;
    return x;
}
Last edited on
Ok, I feel like a moron now haha thanks L B. But question, by using this method it continuously asks, even after the selection was made. But I only call upon it once. Any ideas why it does that?

1
2
3
4
5
6
7
mMenu mainmenu;
	mainmenu.displayMessage();
	mainmenu.menuSelection();
	if(mainmenu.menuSelection() == 1)
	{
	    cout << "Still working on it" << endl;
	}


As you see, I only list the .menuSelecion(); once, but it loops on this stage indefinately.
I dont see anything here to make this loop constantly, so what went wrong?
1
2
3
4
5
6
7
8
int mMenu::menuSelection()
	{
           std::cout << "\nEnter your selection: \n";
           int x;
           std::cin >> x;
      	   return x;

	}
Last edited on
2
3
4
5
	mainmenu.displayMessage();
	mainmenu.menuSelection();
	if(mainmenu.menuSelection() == 1)
	{
By calling it once before the if statement and again in the if statement, you are calling it twice. I don't think it should loop, though.
Topic archived. No new replies allowed.