The following code runs a casino game where the user can choose a starting balan
ID: 3574409 • Letter: T
Question
The following code runs a casino game where the user can choose a starting balance and bet with that amount. Add an option for the player to add some of their winnings to a bank using the following:
A user created class that is also used in the code (don't just create it).
Inheritance of the user created class.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void drawLine(int n, char symbol);
void rules();
int main()
{
string playerName;
int amount; // hold player's balance amount
int bettingAmount;
int guess;
int dice; // hold computer generated number
char choice;
srand(time(0)); // "Seed" the random generator
drawLine(60, '_');
cout << " CASINO GAME ";
drawLine(60, '_');
cout << " Enter Your Name : ";
getline(cin, playerName);
cout << " Enter Deposit amount to play game : $";
cin >> amount;
do
{
system("cls");
rules();
cout << " Your current balance is $ " << amount << " ";
// Get player's betting amount
do
{
cout << playerName << ", enter money to bet : $";
cin >> bettingAmount;
if (bettingAmount > amount)
cout << "Your betting amount is more than your current balance "
<< " Re-enter data ";
} while (bettingAmount > amount);
// Get player's numbers
do
{
cout << "Guess your number to bet between 1 to 10 :";
cin >> guess;
if (guess <= 0 || guess > 10)
cout << "Please check the number!! should be between 1 to 10 "
<< " Re-enter data ";
} while (guess <= 0 || guess> 10);
dice = rand() % 10 + 1; // Will hold the randomly generated integer between 1 and 10
if (dice == guess)
{
cout << " Good Luck!! You won Rs." << bettingAmount * 10;
amount = amount + bettingAmount * 10;
}
else
{
cout << "Bad Luck this time !! You lost $ " << bettingAmount << " ";
amount = amount - bettingAmount;
}
cout << " The winning number was : " << dice << " ";
cout << " " << playerName << ", You have $ " << amount << " ";
if (amount == 0)
{
cout << "You have no money to play ";
break;
}
cout << " -->Do you want to play again (y/n)? ";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
cout << " ";
drawLine(70, '=');
cout << " Thanks for playing game. Your balance amount is $ " << amount << " ";
drawLine(70, '=');
return 0;
}
void drawLine(int n, char symbol)
{
for (int i = 0; i cout<< symbol;
cout << " ";
}
void rules()
{
system("cls");
cout << " ";
drawLine(80, '-');
cout << " RULES OF THE GAME ";
drawLine(80, '-');
cout << " 1. Choose any number between 1 to 10 ";
cout << " 2. If you win you will get 10 times of money you bet ";
cout << " 3. If you bet on wrong number you will lose your betting amount ";
drawLine(80, '-');
}
Explanation / Answer
//working as expected
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void drawLine(int n, char symbol);
void rules();
int main()
{
string playerName;
int amount; // hold player's balance amount
int bettingAmount;
int guess;
int dice; // hold computer generated number
char choice;
int deposit =0 ; //amount to be deposited out of winning
int bankAmount =0; //total bank balance
srand(time(0)); // "Seed" the random generator
drawLine(60, '_');
cout << " CASINO GAME ";
drawLine(60, '_');
cout << " Enter Your Name : ";
getline(cin, playerName);
cout << " Enter Deposit amount to play game : $";
cin >> amount;
char temp;
do
{
system("cls");
rules();
cout << " Your current balance is $ " << amount << " Bank Balance is $ " << bankAmount << " ";
// Get player's betting amount
do
{
cout << playerName << ", enter money to bet : $";
cin >> bettingAmount;
if (bettingAmount > amount)
{
cout << "Your betting amount is more than your current balance Draw money from bank [y/n]" ;
cin >> temp;
if (temp == 'y')
{
if (bettingAmount - amount <= bankAmount)
{
amount = bettingAmount;
bankAmount = bankAmount - (bettingAmount - amount);
}
else
cout << "Not enough money in the your bank account " <<" Re-enter data ";
}
else
cout <<" Re-enter data ";
}
} while (bettingAmount > amount);
// Get player's numbers
do
{
cout << "Guess your number to bet between 1 to 10 :";
cin >> guess;
if (guess <= 0 || guess > 10)
cout << "Please check the number!! should be between 1 to 10 "
<< " Re-enter data ";
} while (guess <= 0 || guess> 10);
dice = rand() % 10 + 1; // Will hold the randomly generated integer between 1 and 10
if (dice == guess)
{
cout << " Good Luck!! You won Rs." << bettingAmount * 10;
cout << " Do you want to add some money to bank from your winning amount(Enter 0 or amount ) ";
cin >> deposit;
if (deposit <= bettingAmount *10)
bankAmount = bankAmount + deposit;
else
amount = amount + (bettingAmount * 10 - deposit);
}
else
{
cout << "Bad Luck this time !! You lost $ " << bettingAmount << " ";
amount = amount - bettingAmount;
}
cout << " The winning number was : " << dice << " ";
cout << " " << playerName << ", You have $ " << amount << " Bank amount $ " << bankAmount << " ";
if (amount + bankAmount == 0)
{
cout << "You have no money to play ";
break;
}
cout << " -->Do you want to play again (y/n)? ";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
cout << " ";
drawLine(70, '=');
cout << " Thanks for playing game. Your balance amount is $ " << amount << " ";
drawLine(70, '=');
return 0;
}
void drawLine(int n, char symbol)
{
for (int i = 0; i< n; i++)
cout<< symbol;
cout << " ";
}
void rules()
{
system("cls");
cout << " ";
drawLine(80, '-');
cout << " RULES OF THE GAME ";
drawLine(80, '-');
cout << " 1. Choose any number between 1 to 10 ";
cout << " 2. If you win you will get 10 times of money you bet ";
cout << " 3. If you bet on wrong number you will lose your betting amount ";
drawLine(80, '-');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.