I am trying to write a simple slot machine program that will return three numbers as the slot icons using a loop. I can get the first part just fine (I think). But I am stuck at converting my icons to intergers or character that I can compare to see if it is a winner or not. Here so far:

// slot machine
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
int Win1 = 0, Win2 = 0, Win3 = 0;

srand (time(0)); // seed random number generator based on time

/* creating a counter and loop to generate three
random numbers */

int c;  // create a counter

for (c = 0; c < 3; c++) //initialize, test and update the counter
{
	int random = rand(); // generate random number
	int Win = (random % 3) + 1; // constrain random number between 1 and 3
	
	cout << Win << " ";
}

return 0;
}

I have tried to add the following to my loop without success:

if (c ==1 )
Win1 = Win;
if (c == 2)
Win2 = Win;
if (c == 3)
Win3 = Win;

I was then going to usr the integers Win1, Win2 etc to make my comparisons but it doesn't work. I feel like I am missing something very simple.

Recommended Answers

All 4 Replies

I am trying to write a simple slot machine program that will return three numbers as the slot icons using a loop. I can get the first part just fine (I think). But I am stuck at converting my icons to intergers or character that I can compare to see if it is a winner or not. Here so far:

// slot machine
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
int Win1 = 0, Win2 = 0, Win3 = 0;

srand (time(0)); // seed random number generator based on time

/* creating a counter and loop to generate three
random numbers */

int c; // create a counter

for (c = 0; c < 3; c++) //initialize, test and update the counter
{
int random = rand(); // generate random number
int Win = (random % 3) + 1; // constrain random number between 1 and 3

cout << Win << " ";
}

return 0;
}


I have tried to add the following to my loop without success:

if (c ==1 )
Win1 = Win;
if (c == 2)
Win2 = Win;
if (c == 3)
Win3 = Win;

I was then going to usr the integers Win1, Win2 etc to make my comparisons but it doesn't work. I feel like I am missing something very simple.

Code tags please:

[code]

// paste code here

[/code]

So you win if you get 1 - 2 -3, otherwise you lose? There is more than one way to do this. One way would be to set up a boolean variable called winner and initialize it to true. Each time through the loop, check to see if the winning number come up. If not, flag winner to false. Any incorrect number that comes up causes winner to be false, so you'll need to initialize winner to true before the loop starts, generate your random number inside the loop, then have an if-statement inside the loop that compares that random number to the number needed to win and if they aren't the same, make winner false. Finally, after the loop, check the winner variable:

bool winner = true;
for (c = 0; c < 3; c++)
{
     // create random number
     if (/* condition */)
          winner = false;

     // code
}

if (winner)
     cout << "You won.";
else
     cout << "You lost.";

Thank you, but the problem I am having, which I still can't solve with your suggestion, is that all 3 numbers that are generated are each the integer "Win". So I can't figure out how to compare them to see if they meet the winning condition. BTW, the winner should be 3 - 3 - 3, or 2 -2 - 2, or 1 - 1 - 1.

Thank you, but the problem I am having, which I still can't solve with your suggestion, is that all 3 numbers that are generated are each the integer "Win". So I can't figure out how to compare them to see if they meet the winning condition. BTW, the winner should be 3 - 3 - 3, or 2 -2 - 2, or 1 - 1 - 1.

Well then there will be no if test for your first number since you can't lose till at least the second number comes up. You'll need to create another variable to hold the first number that comes up, then compare it to the current number.

int firstNumber;
bool winner = true;
for (c = 0; c < 3; c++)
{
     // create a random number

     if (c == 0)
     {
          // code to assign firstNumber to equal random 
          // number from above.
     }
     else if (/* compare random number to firstNumber */)
          winner = false;

     // code
}

if (winner)
     cout << "You won.";
else
     cout << "You lost.";

Or Else Generate 3 Random numbers and then compare if they are equal or not .

You can define 3 variables to hold the numbers and then use if (Statement) to get a true or false. based upon which you can give out a result.

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.