I am having trouble getting my start menu to continue after the user inputs the
ID: 3711204 • Letter: I
Question
I am having trouble getting my start menu to continue after the user inputs the last input. It just goes into a loop and never continues. I have spent many hours trying to get it to move on and am not getting why. Could someone help me get the function to get out of the loop and move on?
Here is my code in c++ for the function:
/*************************************************************************
** start menu function
** this will print the starting menu to the screen
*************************************************************************/
int Ant::startMenu(int choice)
{
cout << "Langton's Ant Simulation Menu ";
cout << "1. Start Langton's Ant Simulation ";
cout << "2. Quit ";
cout << "Enter your choice ";
cin >> choice;
if(choice < 1 || choice >2){
cout << "This is not a valid choice. Please enter 1 or 2. ";
cin >> choice;
}
else if(choice == 1){
cout << "Please enter the number of rows for the board. ";
cin >> bRows;
cout << "Please enter the number of columns for the board. ";
cin >> bCols;
cout << "Please enter the number of steps you would like to have the ant take. ";
cin >> maxSteps;
cout << "Please enter the row number the ant should start on. ";
cin >> startRow;
cout << "Please enter the column number the ant should start on. ";
cin >> startCol;
cout<<"Thank you";
}
else{
cout << "Quiting simulation. "; //user chose to quit simulation
exit(0);
}
cout<<"here";
return 0;
}
Thank you.
Explanation / Answer
int Ant::startMenu(int choice)
{
// infinite loop
while(1)
{
cout << "Langton's Ant Simulation Menu ";
cout << "1. Start Langton's Ant Simulation ";
cout << "2. Quit ";
cout << "Enter your choice ";
cin >> choice;
// if user wants to quit
if( choice == 2 )
{
cout << "Quiting simulation. "; //user chose to quit simulation
exit(0);
}
else if(choice == 1){
cout << "Please enter the number of rows for the board. ";
cin >> bRows;
cout << "Please enter the number of columns for the board. ";
cin >> bCols;
cout << "Please enter the number of steps you would like to have the ant take. ";
cin >> maxSteps;
cout << "Please enter the row number the ant should start on. ";
cin >> startRow;
cout << "Please enter the column number the ant should start on. ";
cin >> startCol;
cout<<"Thank you";
}
else{
cout << "This is not a valid choice. Please enter 1 or 2. ";
cin >> choice;
}
cout<<"here";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.