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:
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.
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;
}
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?
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.
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.
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;
}