Can help? My casino code always fail to build.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    char PlayerName[40]; // Player name
    int b_amount; // Current balance amount.
    int amount_BET; // Amount to be betted.
    int number; // Number bet by a player.
    int r_number; // Number chosen randomly.
    char choice; // Asks a player to play again.

    cout << "Welcome to the Casino!" <<endl;

    cout << "Enter your name (up to 40 characters): ";
    cin >> PlayerName;

    cout<<endl<<endl;

    cout << "Enter the amount to deposit: $";
    cin >> b_amount;

    do
    {

        cout << "Current balance: $"<< b_amount <<endl;

        do
        {
            // GAME START

            cout << "Enter the amount you want to bet: $";
            cin >> amount_BET;

            if (amount_BET > b_amount)
            {
                cout << "The amount you bet is more than your current balance. Please re-enter." <<endl;
            }
            else
            {
                break;
            }
        } while(1);

        do
        {
            // BET A NUMBER

            cout << "Enter a number to bet (1 - 12): ";
            cin >> number;

            if (number < 1 && number > 12)
            {
                cout << "The number you entered is out of range. Please re-enter (1 - 12)." <<endl;
            }
            else
            {
                break;
            }
        } while (1);

        // PROCESS

        srand( time(NULL) );
        r_number = rand(12) + 1;

        // RESULT

        if (r_number == number)
        {
            amount_BET = amount_BET * 10;
            cout << "Lucky! You won $" << amount_BET <<endl;
            b_amount = b_amount + amount_BET;
        }
        else
        {
            cout << "I'm sorry, you didn't win this time." <<endl;
            cout << "You lost $" << amount_BET <<endl;
            b_amount = b_amount - amount_BET;
        }

        cout << "Winning number: " << r_number <<endl;
        cout << "Current balance: $" << b_amount <<endl<<endl;

        cout << "Do you want to play again? (y/n): ";
        cin >> choice;
    } while (choice = 'y');

    cout << "Current balance: $" << b_amount <<endl;
    cout << "Thank you for playing!" <<endl;

    system ("pause");
    return 0;
}

Recommended Answers

All 8 Replies

Whats the error?

Always have srand and rand error

Your call to srand should be outside your loop. I would suggest putting it on line 15. As for you call to rand() it should look like this: r_number = rand() % 12 + 1;. Your if condition on line 55 is also incorrect. Can a number be less than 1 and greater then 12 at the same time?

And, if you are writing a casino game, then I would suggest you research the topic of Monte Carlo algorithms. This is where your code is headed, so you might as well do it properly. :-)

So, here is a great site for these subject, both Monte Carlo algorithms as well as random number generators: http://www.phy.ornl.gov/csep/CSEP/BMAP.html

do{

    ....
    }while(1);

Can someone explain what does that condition in the do while loop mean?

It means run forever since 1 will always be true. BTW you should start your own discussion thread to ask a question about something different.

//cout << "Enter a number to bet (1 - 12): ";

if (number < 1 && number > 12)
{
    cout << "The number you entered is out of range. Please re-enter (1 - 12)." <<endl;
}
else
{
    break;
}

I think you have a logic flaw here...shouldn't it be

if (number >= 1 && number <= 12) //????

and

r_number = rand(12) + 1;

note

time_t rand(void);

//rand() takes not parameter!

if your intention is to get a random number from 1 to 12 then change it to

r_number = (rand() % 12) + 1;

also

while (choice = 'y');

should be

while (choice == 'y'); //note the `double equal sign!

@NathanOliver Thank you. I thought to ask in this discussion thread since it was in the provided code. Anyways, I will keep that in mind next time :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.