Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why does my function called \"menu\" keep repeating no matter what input is put

ID: 3725189 • Letter: W

Question

Why does my function called "menu" keep repeating no matter what input is put in? How do I fix this?

#include <iostream>

#include <cmath>

#include <string>

#include <iomanip>

using namespace std;

string Menu(string userSelect) {

   cout << "Menu: Please select one of the following options:" << endl << endl;

   cout << "1 - Drop a single chip into one slot" << endl;

   cout << "2 - Drop multiple chips into one slot" << endl;

   cout << "3 - Show the options menu" << endl;

   cout << "4 - Quit the program" << endl << endl;

   cout << "Enter your selection now: ";

   cin >> userSelect;

   cout << endl << endl;

   return userSelect;

}

int main()

{

   string userSelect = " ";

   cout << "Welcome to the Plinko simulator! Enter 3 to see options." << endl << endl;

   cin >> userSelect;

   do {

while (userSelect != "1" && userSelect != "2" && userSelect != "3" && userSelect != "4") {

           cout << "Invalid selection. Enter 3 to see options." << endl << endl;

           cout << "Enter your selection now: ";

           cin >> userSelect;

           cout << endl << endl;

       }

       if (userSelect == "1") {

           //ADD CODE LATER

       }

       if (userSelect == "2") {

           //ADD CODE LATER

       }

       if (userSelect == "3") {

           Menu(userSelect);

       }

       if (userSelect == "4") {

           //ADD CODE LATER

       }

   } while (userSelect != "4");

   return 0;

}

Explanation / Answer

Use userSelect=Menu(userSelect); in place of Menu(userSelect);

Then program will run correctly.

Explanation:

Reason behind all this is:

cin >> userSelect; this is accepting input under Menu method and returning input value as

return userSelect;

caller Menu(userSelect); has no any assignment and still it is holding old value. That's why Menu method is getting invoked again and again.

So to avoid this use:

userSelect = Menu(userSelect);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote