This is a Slot Machine Simulation and I keep getting an error at the end of the
ID: 3722949 • Letter: T
Question
This is a Slot Machine Simulation and I keep getting an error at the end of the main module. (//pause system system("Pause"); return 0; }//end main() this is the section that keeps giving me an error. Please help my code is below.
#include<cstdlib>
#include<iostream>
#include<string>
#include<cmath>
#include<iomanip>
//declare variables
int spin(int);
void playGame();
float insertMoney(float);
int showSpin(int, int, int);
int showSymbol(int);
int getMatches(int, int, int);
float calcWinnings(int, float);
char wantToPlayAgain(char);
using namespace std;
// Global constants to represent the slot machine symbols
const int CHERRIES = 1;
const int ORANGES = 2;
const int PLUMS = 3;
const int BELLS = 4;
const int MELONS = 5;
const int BARS = 6;
int main()
{
// The again variable is used to determine whether the
// user wants to continue playing the game.
char again;
float money;
// Start playing?
do
{
// Pressing a key spins the machine.
cout << "Press Enter to spin? " << endl;
cin.get();
// Simulate the spin.
playGame();
// Does the user want to play again?
while (wantToPlayAgain(again) == 'Y');
}//end do
//pause system
system("Pause");
return 0;
}//end main()
// The playGame module plays one spin of the slot machine.
void playGame()
{
//declaration of variables
int symbol1, symbol2, symbol3, matches;
float money, winnings;
money = 0;
//insert the money
money = insertMoney(money);
//spin
symbol1 = spin(symbol1);
symbol2 = spin(symbol2);
symbol3 = spin(symbol3);
//show the resulting symbols
showSpin(symbol1,symbol2,symbol3);
// Get the number of matching symbols.
matches = getMatches(symbol1,symbol2,symbol3);
// Get the game winnings.
winnings = calcWinnings(matches, money);
// Display the winnings.
cout << "You Won $" << setprecision(2) << fixed << winnings << endl << endl;
}//end module(playGame)
//insertMoney module
float insertMoney(float money)
{
cout << "How much money do you want to insert?";
cin >> money;
return money;
}//end module (insertMoney)
//spin module
int spin(int symbol1)
{
symbol1 = rand() % BARS + CHERRIES;
return symbol1;
}//end module(spin)
// The showSymbol module displays a slot symbol as a word.
int showSpin(int symbol1, int symbol2, int symbol3)
{
showSymbol(symbol1);
showSymbol(symbol2);
showSymbol(symbol3);
}//end module(showSpin)
// The showSymbol module displays a slot symbol as a word.
int showSymbol(int symbol)
{
switch (symbol)
{
case 1:
cout << "Cherries ";
break;
case 2:
cout << "Oranges ";
break;
case 3:
cout << "Plums ";
break;
case 4:
cout << "Bells ";
break;
case 5:
cout << "Melons ";
break;
case 6:
cout << "Bars ";
break;
}//end module(switch)
}//end module(showSymbol)
// The getMatches function accepts arguments for the three
// slot symbols and returns the number of them that match.
int getMatches(int symbol1, int symbol2, int symbol3)
{
int matches;
// Do all three symbols match?
if (symbol1 == symbol2 && symbol1 == symbol3)
{
matches = 3;
}//end if
// Else, do two of the symbols match?
else if (symbol1 == symbol2 || symbol1 == symbol3 || symbol2 == symbol3)
matches = 2;
// Otherwise, there are no matches.
else
{
matches = 0;
}//end else
// Return the number of matches.
return matches;
}//module(getMatches)
// The calcWinnings function accepts the number of matches and
// the amount of money inserted as arguments, and returns the
// amount of money won for this spin.
float calcWinnings(int matches, float money)
{
return matches * money;
}//end float(calcWinnings)
// The wantToPlayAgain module asks the user if he or she wants to
// play again and stores the input as "Y" or "N" in the reference
// parameter.
char wantToPlayAgain(char again)
{
// Ask the user?
cout << "Do you want to play again? enter Y to continue or N to quit ";
cin >> again;
// Validate the input.
while (again != 'Y' && again != 'N')
{
cout << "Enter either Y or N. ";
cin >> again;
}//end while
}//end module(wantToPlayAgain)
Explanation / Answer
Checked above program and found that do-while() loop is used for looping and while is written before close braces "}"
corrected while loop and executed, program executed successfully .
I am not sure about the functionality of the program but it is compiled and executed.
do
{
// Pressing a key spins the machine.
cout << "Press Enter to spin? " << endl;
cin.get();
// Simulate the spin.
playGame();
// Does the user want to play again?
}while (wantToPlayAgain(again) == 'Y');
Could you please use above block of logic in main, compile & execute
do
{
// Pressing a key spins the machine.
cout << "Press Enter to spin? " << endl;
cin.get();
// Simulate the spin.
playGame();
// Does the user want to play again?
}while (wantToPlayAgain(again) == 'Y');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.