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

2. Run-Time Errors in Loops and Branching Loops and branching are hot spots for

ID: 3862035 • Letter: 2

Question

2. Run-Time Errors in Loops and Branching

Loops and branching are hot spots for simple but difficult-to-spot bugs. This code is filled with some very annoying and common bugs. The code will compile fine, but it won’t work right and may even crash. There are two bugs that are crash bugs. There is one logic error that causes the program to quit before it should and one typo that causes incorrect behavior. This last one will be the hardest to find because it only happens when you win, and it is an error that is notoriously hard to spot until you’ve been caught by it enough to expect it.

This is a simple slot machine. Here is how it is supposed to work.

When you hit the return key, the wheel spins (you won’t actually see any spinning, just the results of the spin).

You automatically bet $1 on each spin.

If you get three of the same symbols, you win according to the following table.

Lemons—you keep your bet.

Cherries—you win $5.

Oranges—you win $10.

Bells—you win $15.

Jackpot—you win $1,000 and the game is over.

When your pot is gone (pot == 0), you lose and the game is over.

Here is the code.

// Week 3 Assignment- 2

// Description: Run-time errors in loops and branching

//----------------------------------

//**begin #include files************

#include <iostream> // provides access to cin and cout

#include <iomanip>

#include <array>

#include <vector>

//--end of #include files-----------

//----------------------------------

using namespace std;

//----------------------------------

//**begin main program**************

int main()

{

       // seed random number generator

       srand(time(NULL));

       // create enum for symbols

       enum symbol

       {

              Lemon, Cherry, Orange, Bell, Jackpot

       };

       // create a struct for slot machine wheel

       struct Wheel

       {

              array <string, 10> symbols;

              array <symbol, 10> eSymbols;

              int position;

              string selected;

       };

       //create an array of three slot machine wheels

       array <Wheel, 3> slotMachine =

       {

              {

                     {

                           {"Cherry", "Orange", "Lemon", "Orange", "Bell", "Orange", "Lemon", "Cherry", "Jackpot", "Bell"},

                           {Cherry, Orange, Lemon, Orange, Bell, Orange, Lemon,Cherry, Jackpot, Bell},

                           0,"Cherry"

                     },

                     {

                           {"Cherry", "Bell", "Lemon", "Orange", "Bell", "Jackpot", "Lemon", "Cherry", "Jackpot", "Bell"},

                           {Cherry, Bell, Lemon, Orange, Bell, Jackpot, Lemon, Cherry, Jackpot, Bell},

                           1,"Bell"

                           },

                           {

                                  {"Cherry", "Orange", "Lemon", "Orange", "Lemon", "Orange", "Lemon","Cherry", "Jackpot", "Bell"},

                                  {Cherry, Orange, Lemon, Orange, Lemon, Orange, Lemon, Cherry, Jackpot, Bell},

                                  2,"Lemon"

                           }

              }

       };

       bool gameOn = true;

       bool winner = false;

       int thePot = 100;

       int bet = 1;

       vector <int> combo;

       while (gameOn)

       {

              for (int i = 1; i < 4; i++)

              {

                     slotMachine[i].position =(slotMachine[i].position + rand()%10)%10;

                     slotMachine[i].selected = slotMachine[i].symbols[slotMachine[i].position];

                     cout << setw(10) << left << slotMachine[i].selected.c_str() ;

                     combo.push_back(slotMachine[i].eSymbols[slotMachine[i].position]);

              }

              if ((combo[0] == combo[1]) && (combo[1] == combo[2]))

              {

                     if (combo[0] == Lemon)

                     {

                           cout << "You keep your bet." << endl;

                     }

                     else if(combo[0] = Jackpot)

                     {

                           cout << "**** You hit $1000 Jackpot!!! ****" << endl;

                           thePot += 1000;

                           winner = true;

                           gameOn = false;

                     }

                     else

                     {

                           cout << "WINNER! You win $" << combo[0]*5 << endl;

                           thePot += combo[0]*5;

                     }

              }

              else

              {

                     thePot -= bet;

                     if (thePot > 0 ) gameOn=false;

              }

              cout << "You now have $" << thePot << endl;

              combo.clear();

              cout << endl;

              cin.get();

       }

       if (winner) cout << "You walk away a winner." << endl;

       else cout << "You have lost all your money." << endl;

       // Wait for user input to close program when debugging.

       cin.get();

       return 0;

}

//--end of main program-------------

//----------------------------------

Explanation / Answer

// Week 3 Assignment- 2
// Description: Run-time errors in loops and branching
//----------------------------------

//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <iomanip>
#include <array>
#include <vector>

//--end of #include files-----------
//----------------------------------

using namespace std;
//----------------------------------


//**begin main program**************
int main()
{
// seed random number generator
srand(time(NULL));
// create enum for symbols
enum symbol
{
Lemon, Cherry, Orange, Bell, Jackpot
};

// create a struct for slot machine wheel
struct Wheel
{
array <string, 10> symbols;
array <symbol, 10> eSymbols;
int position;
string selected;
};
//create an array of three slot machine wheels
array <Wheel, 3> slotMachine =
{
{
{
{"Cherry", "Orange", "Lemon", "Orange", "Bell", "Orange", "Lemon", "Cherry", "Jackpot", "Bell"},
{Cherry, Orange, Lemon, Orange, Bell, Orange, Lemon,Cherry, Jackpot, Bell},
0,"Cherry"
},
{
{"Cherry", "Bell", "Lemon", "Orange", "Bell", "Jackpot", "Lemon", "Cherry", "Jackpot", "Bell"},
{Cherry, Bell, Lemon, Orange, Bell, Jackpot, Lemon, Cherry, Jackpot, Bell},
1,"Bell"
},
{
{"Cherry", "Orange", "Lemon", "Orange", "Lemon", "Orange", "Lemon","Cherry", "Jackpot", "Bell"},
{Cherry, Orange, Lemon, Orange, Lemon, Orange, Lemon, Cherry, Jackpot, Bell},
2,"Lemon"
}
}
};

bool gameOn = true;
bool winner = false;
int thePot = 100;
int bet = 1;
vector <int> combo;
while (gameOn)
{
for (int i = 0; i < 3; i++) // two error causing crash. i = 0 is intialiser and as array has 3 element it should loop till 3
{
slotMachine[i].position =(slotMachine[i].position + rand()%10)%10;
slotMachine[i].selected = slotMachine[i].symbols[slotMachine[i].position];
cout << setw(10) << left << slotMachine[i].selected.c_str() ;
combo.push_back(slotMachine[i].eSymbols[slotMachine[i].position]);
}
if ((combo[0] == combo[1]) && (combo[1] == combo[2]))
{
if (combo[0] == Lemon)
{
cout << "You keep your bet." << endl;
}
else if(combo[0] == Jackpot) // <- type error instead of == specified
{
cout << "**** You hit $1000 Jackpot!!! ****" << endl;
thePot += 1000;
winner = true;
gameOn = false;
}
else
{
cout << "WINNER! You win $" << combo[0]*5 << endl;
thePot += combo[0]*5;
}
}
else
{
thePot -= bet;
if (thePot <= 0 ) gameOn=false; // incorrect logic. On any loose it was quiting
}
cout << "You now have $" << thePot << endl;
combo.clear();
cout << endl;
cin.get();
}
if (winner) cout << "You walk away a winner." << endl;
else cout << "You have lost all your money." << endl;
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//--end of main program-------------
//----------------------------------

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