I am making a simple amusement park simulation for a class assignment but am having trouble looping statements inside of a switch. I have tagged my coding below
#include <iostream>
#include "CinReader.h"
#include <fstream>
usingnamespace std;
CinReader reader;
string username;
void printImage (string imageFile);
void greeting ();
void entrance ();
void bathroom ();
void hotDogStand ();
void rides ();
void arcade ();
int main()
{
greeting ();
entrance ();
bathroom ();
hotDogStand ();
/* rides ();
arcade ();
*/
return 0;
}
void greeting ()
{
cout << " ********************************\n";
cout << " *** WELCOME, ENTER YOUR NAME ***\n";
cout << " ********************************\n\n";
username = reader.readString();
cout << "Well " << username << ", Let's visit an amusement park today.\n\n";
cout << "-You walk across the entrance bridge into the open entrance yard of the park.\n";
cout << "-The map below shows you your choices of sections in the park to visit.\n\n";
}
void entrance ()
{
int choice = 0;
do
{
cout << " *BATHROOM* *HOT DOG STAND* *RIDES* *ARCADE*\n";
cout << " | | | | \n";
cout << " | | | | \n";
cout << " | | ********** | | \n";
cout << " --------------------*ENTRANCE*-------------- \n";
cout << " ********** \n\n";
cout << " What Would You Like To Do?\n\n";
cout << "[1] Go to the Bathroom\n";
cout << "[2] Get some food at the Hot Dog Stand\n";
cout << "[3] Go on some rides\n";
cout << "[4] Play some games in the arcade\n";
cout << "[0] LEAVE THE PARK\n\n";
cout << "enter your choice\n\n";
choice = reader.readInt (0,4);
switch (choice)
{
case 1:
bathroom ();
break;
case 2:
hotDogStand ();
break;
// case 3:
// rides ();
// break;
// case 4:
// arcade ();
// break;
case 0:
cout << "Now Leaving the Amusement Park. Please Come Again\n\n";
// quit = true;
break;
}
}while (choice != 0);
}
void bathroom ()
{
int bathroomChoice = 0;
do
{
staticbool lightsOn = false;
if (lightsOn == true)
{
cout << "\nFilthy Bathroom.\n\n";
cout << " What Next?\n";
cout << " 1. Use the urinal\n";
cout << " 2. Use a stall\n";
cout << " 3. Wash your hands\n";
cout << " 4. Go back to the Entrance\n";
bathroomChoice = reader.readInt (1,4);
char answer = 'y';
answer = reader.readChar();
if (toupper(answer) == 'Y')
lightsOn = false;
}
else
{
cout << "\nThe room is pitch black.\n\n";
cout << "Turn on the lights (y/n)\n\n";
char answer = 'y';
answer = reader.readChar();
if (toupper(answer) == 'Y')
lightsOn = true;
}
switch (bathroomChoice)
{
case 1:
cout << " You finish your business. Don't forget to wash your hands!!\n\n";
break;
case 2:
cout << " You open the door to the bathroom stall\n";
cout << " The smell is so horrid you think about turning around\n";
cout << " But you get the job done and leave the wretched stall\n\n";
break;
case 3:
cout << " You wash your hands in what you hope is water and dry them with the air dryer.\n";
cout << " Your hands are still somewhat wet. Air dryers suck ass.\n";
break;
case 4:
entrance();
break;
}
}while (bathroom != 0);
}
void hotDogStand ()
{
int foodChoice = 0;
int hotdogPicked = 0;
int sodaPicked = 0;
int funnelcakePicked = 0;
int corndogPicked = 0;
int milkshakePicked = 0;
printImage ("HotDogShack.txt");
cout << "\nWhat can I prepare for you\n\n";
cout << " 1. Hot Dog\n";
cout << " 2. Soda\n";
cout << " 3. Funnel Cake\n";
cout << " 4. Corn Dog\n";
cout << " 5. Milkshake\n";
cout << " 6. Return to the Entrance\n\n";
cout << " Please Choose\n\n";
foodChoice = reader.readInt();
switch (foodChoice)
do
{
{
case 1:
cout << "One Hot Dog Coming Up...\n\n";
hotdogPicked ++;
break;
case 2:
cout << "One soda coming up...\n\n";
sodaPicked ++;
break;
case 3:
cout << "One funnel cake coming up...\n\n";
funnelcakePicked ++;
break;
case 4:
cout << "One Corn Dog coming up...\n\n";
corndogPicked ++;
break;
case 5:
cout << "One Milkshake coming up...\n\n";
milkshakePicked ++;
break;
case 6:
cout << "You consumed\n";
cout << "...... " << hotdogPicked << " hot dogs\n";
cout << "...... " << sodaPicked << " sodas\n";
cout << "...... " << funnelcakePicked << " funnel cakes\n";
cout << "...... " << corndogPicked << " corn dogs\n";
cout << "...... " << milkshakePicked << " milkshakes\n\n";
cout << "You are now returning to the entrance\n\n";
entrance ();
break;
}
}while (foodChoice = (1,5));
}
void printImage (string imageFile)
{
ifstream fin(imageFile.c_str());
if (!fin.fail())
{
char nextChar = fin.get();
while (nextChar != EOF)
{
cout << nextChar;
nextChar = fin.get();
}
fin.close();
}
}
The hot dog stand is where I am stuck. I want the user to be able to choose as many items as possible. Then take them back to the options of food until they choose to leave. In which I want them to return to the entrance function. The hot dog stand code is below.
The switch is disconnected from its { } block. It needs to be within the do. You also need to put the call to readInt inside the do at the top. The while should be more like this, while(foodChoice <= 5 && foodChoice >= 1);
What happens when entrance returns? I think that you need to draw some flow charts so that you understand the flow of your program. It seems like entrance is more of a main menu so what you really want is for each of the other functions to return back to main. All functions will eventually return to their caller. You probably want a loop in main that prints main menu over and over and after each function returns you simply print the entrance menu again.