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

I\'m trying to prompt the program to \"drop a single chip\". I get to the \"Sing

ID: 3725612 • Letter: I

Question

I'm trying to prompt the program to "drop a single chip". I get to the "SingleDrop" function and then it calls the "SlotCheck" function. When I get to the "SlotCheck" function, nothing goes right. When I put in a valid slot number 0-8, the "SingleDrop" function repeats continuosly. When I put in an invalid slot number, I get this error pictured below...

How do I fix these problems?

this is my code:

#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;

}

string EnterSelection(string userSelect) {

   cout << "Enter your selection now: " << endl << endl;

   cin >> userSelect;

   return userSelect;

}

int SlotCheck(int numSlot) {

   cin >> numSlot;

   if (numSlot < 0 || numSlot > 8)

   {

       cout << "Invalid slot." << endl << endl;

       string userSelect = EnterSelection(userSelect);

   }

   else {

       cout << "vaild slot" << endl << endl;

   }

   return numSlot;

}

void SingleDrop() {

   cout << "*** Drop a single chip ***" << endl << endl;

   cout << "Which slot do you want to drop the chip in (0-8)? " << endl << endl;

   int numSlot = 9;

   numSlot = SlotCheck(numSlot);

}

int main()

{

   string userSelect = " ";

   int numSlot = 0;

   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") {

           SingleDrop();

       }

       if (userSelect == "2") {

           //ADD CODE LATER

       }

       if (userSelect == "3") {

           userSelect = Menu(userSelect);

       }

       if (userSelect == "4") {

           //ADD CODE LATER

       }

   } while (userSelect != "4");

   return 0;

}

int SlotCheck(int numSlot) cin numSlot; cout

Explanation / Answer

//This is happening because in SlotCheck() function you were trying to call function with same local varialbe.

string userSelect = EnterSelection(userSelect); => both side userSelect is same , in function call you are accessing local variable before it declaration. so to avoid this userSelect must be a global variable But

you want to call it from SlotCheck() function and want to read string and SlotCheck() function returns string so

It is suggested that return an indicator from SlotCheck() like -1 and handle it in Single drop function like I did.

//Please find your working code below, it same with just one small modification

*****************************************************************************************

//Your Program start from below line.

//#include "stdafx.h"

#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;

}

string EnterSelection(string userSelect) {

cout << "Enter your selection now: " << endl << endl;

cin >> userSelect;

return userSelect;

}

int SlotCheck(int numSlot) {

cin >> numSlot;

if (numSlot < 0 || numSlot > 8)

{

cout << "Invalid slot." << endl << endl;

//Below line creating problem. you are sending local userSelect variable before it declaration

//string userSelect = EnterSelection(userSelect);

return -1;

}

else {

cout << "vaild slot" << endl << endl;

}

return numSlot;

}

void SingleDrop() {

cout << "*** Drop a single chip ***" << endl << endl;

cout << "Which slot do you want to drop the chip in (0-8)? " << endl << endl;

int numSlot = 9;

numSlot = SlotCheck(numSlot);

if (numSlot == -1)

{

SingleDrop();

}

}

int main()

{

string userSelect = " ";

int numSlot = 0;

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") {

SingleDrop();

}

if (userSelect == "2") {

//ADD CODE LATER

}

if (userSelect == "3") {

userSelect = Menu(userSelect);

}

if (userSelect == "4") {

//ADD CODE LATER

}

} while (userSelect != "4");

return 0;

}