2

I got a small program where in the end, the program asks the user if he/she wants to roll dice to win an extra 15% off their initial check, but my if statement is not recognizing that if the user rolls a 6, they win the discount. When the dice eventually rolls a 6, it still reads as a fail and tells the user to pay the full amount. How can I work around this?

My class:

class roll
{
private:
    int high;
public:
    roll(int high = 6)
    {
        this->high = high;
    }

    ~roll()
    {

    }

    int rolled(int amt = 1)
    {
        int done = 0;

        for (size_t x = 0; x < amt; x++)
        {
            done += rand() % high + 1;
        }
        return done;
    }

};

My if statement:

  cout << "Would you like to play a dice game for a discount? Y/N: " << endl;
            cin >> res;
            if (res == 'Y' || res == 'y')
            {
                srand(time(static_cast<unsigned>(0)));
                roll one;
                cout << one.rolled() << endl;
                if (one.rolled() == 6)
                {
                    cout << "Congratulations!  You won 15% off your meal!!!" << endl;
                    prize = grandtot - (grandtot * .15);
                    cout << "Your final total will be $" << prize << endl;
                }
                else
                {
                    cout << "Sorry, you did not win, pay the original amount!" << endl;
                }
            }
            else
            {
                cout << "Thank you, pay the original amount and have a nice day!" << endl;
            }
10
  • 3
    Remember that rolled will (usually) return a different number if you call it a second time. (I think everyone falls into this trap more or less regularly.)
    – molbdnilo
    Commented May 17, 2019 at 12:15
  • 1
    prize = grandtot - (grandtot * .15); is equivalent to prize = grandtot * .85; Commented May 17, 2019 at 12:17
  • 2
    srand(time(static_cast<unsigned>(0))); You should call this 1 time at the beginning of main and avoid putting it in a loop. Remember that the resolution of the clock is 1 second. And also if the seed is the same you get the same random sequence. Meaning if this loop executes fast enough you will get the same roll multiple times in a row.
    – drescherjm
    Commented May 17, 2019 at 12:18
  • 1
    class roll is completely unnecessary. It could have been a simple function.
    – UmNyobe
    Commented May 17, 2019 at 12:20
  • 3
    Please don't use rand(). There's whole c++11 new library <random> that does RNG correctly.
    – Quimby
    Commented May 17, 2019 at 12:26

2 Answers 2

6

Basically, look at @PaulEvans answer for your question. I want to put some focus on your rolled function:

int rolled(int amt = 1)
{
    int done = 0;

    for (size_t x = 0; x < amt; x++)
    {
        done += rand() % high + 1; // <= This line
    }
    return done;
}

Pay attention that you are using rand function to get random values. It's true that you can get random values by using this function, but I would recommend to use C++11 way - with better distribution (don't forget #include ):

int rolled(int amt = 1)
{
    int done = 0;
    std::random_device dev;
    std::mt19937 rng(dev());
    std::uniform_int_distribution<std::mt19937::result_type> dist6(1,6); // distribution in range [1, 6]

    for (size_t x = 0; x < amt; x++)
    {
        done += dist6(rng); // <= This line
    }
    return done;
}

For more details see: https://stackoverflow.com/a/13445752/8038186

0
5

You're not storing your roll, you want this instead:

const int current_roll = one.rolled();
cout << current_roll << endl;
if (current_roll == 6)
...
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.