Write a C program that simulates a dice game Prompt the user

Write a C++ program that simulates a dice game. Prompt the user for a betting balance. Then start the game. Prompt the player for a bet amount. The amount has to be less or equal to the bet balance. The player cannot bet with a $0 balance. If the player rolls two dice that sum anywhere between 2 and 6 the player wins the bet, and the bet amount is added to the betting balance. If the player rolls a total between 7 and 12 the player loses, and the bet amount is subtracted from the betting balance. After each roll the player is asked if they want to continue playing. If they decide to stop display the ending balance with a friendly message. If the balance is greater then what they started with congratulate them on winning. If the amount is less than what they started with display a message that wishes them better luck next time. You are required to use a method to roll a six sided. You are also required to write a method that accepts a valid bet from the user. If the use enters an invalid bet the method will display a message letting the user know, and prompt again for a valid bet. If the user enters three invalid bets in a row the user is asked to leave the casino, and the game ends. The final balance should still be displayed. Please enter your starting balance in whole dollars: 100 Your beginning balance is $100, good luck!! Enter a valid bet: 50 The roll is 2 and 3 for a 5, you win! Your balance is $150 Do you want to continue (Y/N)? Y Enter a valid bet: 200 That is more than your balance Enter a valid bet: 100 The roll is 6 and 6 for a 12, you lose Your balance is $50 Do you want to continue (Y/N)? N You have $50 Better luck next time! Have a wonderful evening!

Solution

// C++ dice game

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

// roll dice
int roll()
{
    int dice = rand()%6 + 1;
    return dice;
}

// input valid bet
int validBet(int balance)
{
    int bet;
    int count = 0;
    while(true)
    {
        cout << \"Enter a valid bet: \";
        cin >> bet;

        count++;

        if(bet <= 0 || bet > balance)
            cout << \"Invalid bet\ \";
        else
            break;

        if(count == 3)
        {
            cout << \"Final balance: $\" << balance << \"\ \";
            cout << \"Kindly leave the casino\ \";
            return 0;
        }
    }

    return bet;
}


int main()
{
    srand(time(0)); // \"Seed\" the random generator

    int initialbalance;
    int balance;
    int bettingAmount;
    int dice1;
    int dice2;
    int sum;
  
    cout << \"\ \ Please Enter your starting balance in whole dollars: \";
    cin >> initialbalance;
    balance = initialbalance;
    cout << \"Your beginning balance is $\" << balance << \", good luck!!\ \ \";
  
    while(true)
    {
        bettingAmount = validBet(balance);
        if (bettingAmount == 0)
        {
            return 0;
        }

        dice1 = roll();// Will hold the randomly generated integer between 1 and 6
        dice2 = roll(); // Will hold the randomly generated integer between 1 and 6

        sum = dice1 + dice2;

        if(sum >= 2 && sum <= 6)
        {
            cout << \"The roll is \" << dice1 << \" and \" << dice2 << \" for a \" << sum << \", you win!\ \";
            balance = balance + bettingAmount;
        }
        else if(sum >= 7 && sum <= 12)
        {
            cout << \"The roll is \" << dice1 << \" and \" << dice2 << \" for a \" << sum << \", you lose!\ \";
            balance = balance - bettingAmount;
        }

        cout << \"Your balance is $\" << balance << endl;

        char choice;
        cout << \"\ \ -->Do you want to continue (y/n)? \";      
        cin >> choice;

        if(choice == \'n\' || choice == \'N\')
            break;

    }

    if(balance > initialbalance)
    {
        cout << \"You have $\" << balance << endl;
        cout << \"Well done! Have a wonderful evening!\ \";
    }
    else
    {
        cout << \"You have $\" << balance << endl;
        cout << \"Better luck next time! Have a wonderful evening!\ \";
    }


return 0;
}


/*
output:

Please Enter your starting balance in whole dollars: 100
Your beginning balance is $100, good luck!!

Enter a valid bet: 110
Invalid bet
Enter a valid bet: 50
The roll is 1 and 5 for a 6, you win!
Your balance is $150


-->Do you want to continue (y/n)? y
Enter a valid bet: 110
The roll is 5 and 5 for a 10, you lose!
Your balance is $40


-->Do you want to continue (y/n)? y
Enter a valid bet: 200
Invalid bet
Enter a valid bet: 10
The roll is 3 and 5 for a 8, you lose!
Your balance is $30


-->Do you want to continue (y/n)? n
You have $30
Better luck next time! Have a wonderful evening!

*/

 Write a C++ program that simulates a dice game. Prompt the user for a betting balance. Then start the game. Prompt the player for a bet amount. The amount has
 Write a C++ program that simulates a dice game. Prompt the user for a betting balance. Then start the game. Prompt the player for a bet amount. The amount has
 Write a C++ program that simulates a dice game. Prompt the user for a betting balance. Then start the game. Prompt the player for a bet amount. The amount has

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site