dimanche 28 juin 2015

(C++) Random numbers are equal, but program says they aren't

I am a beginner, and I created a slot machine simulator. The wheel is spun and x, y, and z are set to random numbers. I have an if statement that checks to see if x == y == z. When the program runs, and the numbers are in fact equal, it runs my code that says they are not equal. Why does this happen?

For example, the cout statement will say 1 -- 1 -- 1, then it goes to my if statement for when they are not equal, and I get the "you lose" code.

I should add, this does not happen every time. Sometimes it properly executes the if statement for when they are equal. It is very odd.

    srand(time(0));

    int x = (rand() % 2) + 1;
    int y = (rand() % 2) + 1;
    int z = (rand() % 2) + 1;

    std::cout << "The spin results are... " << x << " -- " << y << " -- " << z << std::endl << std::endl;

    if( x == y == z)
    {
    std::cout << "You win!\n\n";
    playerCoins = playerCoins + (coinsBet * 2);
    }
    else 
    {   
        std::cout << "You lose!\n\n";
    }




Aucun commentaire:

Enregistrer un commentaire