Slot Machine

Hello everybody. I am trying to make a basic slot machine program that generates three(3) random numbers when the "spin" is typed and entered, and ends when all three numbers equal five(5). Here is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

string a= "spin";

int main(int argc, char *argv[])
{
  srand(time(NULL));
  int myarray[3];
  cout << "Type spin" << endl;
  while (myarray[0], myarray [1], myarray[2] !=5)
  {
    cin >> a;
    myarray[0] = rand() % 10;
    myarray[1] = rand() % 10;
    myarray[2] = rand() % 10;
    cout << myarray[0] << myarray[1] << myarray[2] << endl;
  }
  cout << "Winner!" << endl;  
  system("PAUSE");	
  return 0;
}


Now, the problem I'm having is, it seems to end randomly, and you don't have to type "spin" to make the loop run. Could someone shed some light on the situation for a confused beginner? Thanks in advance for any and all advice.
Your loop condition does not do what you think it does.
It should look like this:
while (myarray[0] != 5 && myarray[1] != 5 && myarray[2] !=5)
You should use a do-while loop since the spinning has to happen at least once. If accidentally all three elements of myarray is 5, then your program won't run.

cin >> a; will delete the contents of a and replace it with the next word what is typed in by the user. You should check for equality here :
1
2
3
4
5
6
//in your loop
string answer;
cin >> answer;
if (answer != "spin") { 
    //Do what you wanna do when not "spin" is entered.
}


Also use more self-describing variable names.
And don't use system("PAUSE"), use std::cin.get();
The program still seems to end before all three arrays reach 5... Or is it just not showing that all three equal 5? Everything else seems to work fine though.

Thus far I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

string answer;

int main(int argc, char *argv[])
{
  srand(time(NULL));
  int myarray[3];
  cout << "Type spin" << endl;
  do 
  {
    cin >> answer;
    cin.ignore();
    if (answer == "spin")
    {
    myarray[0] = rand() % 10;
    myarray[1] = rand() % 10;
    myarray[2] = rand() % 10;
    cout << myarray[0] << myarray[1] << myarray[2] << endl;
    }
  }
  while (myarray[0] != 5 && myarray[1] != 5 && myarray[2] !=5);
  if (myarray[0] == 5 && myarray[1] == 5 && myarray[2] ==5) 
  {
    cout << myarray[0] << myarray[1] << myarray[2] << endl;
    cout << "Winner!" << endl;
  }   
  system("PAUSE");	
  return 0;
}


Last edited on
ROmai's solution is wrong.

myarray[0] != 5 || myarray[1] != 5 || myarray[2] != 5


Three conditions are:
A = myarray[0] == 5
B = myarray[1] == 5
C = myarray[2] == 5

End condition is when A and B and C are true.
In other words,

A && B && C

Your loop wants to continue looping while A && B && C is FALSE.
In other words, !( A && B && C ).

Your loop as written is !A && !B && !C, which is not the same
as !(A && B && C).






And it works perfect! Thank you everybody who contributed. And here's the final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

string answer;

int main(int argc, char *argv[])
{
  srand(time(NULL));
  int myarray[3];
  cout << "Type spin" << endl;
  do 
  {
    cin >> answer;
    cin.ignore();
    if (answer == "spin")
    {
    myarray[0] = rand() % 6;
    myarray[1] = rand() % 6;
    myarray[2] = rand() % 6;
    cout << myarray[0] << myarray[1] << myarray[2] << endl;
    }
  }
  while (!(myarray[0]==5&&myarray[1]==5&&myarray[2]==5));
  if (myarray[0] == 5 && myarray[1] == 5 && myarray[2] == 5) 
  {
    cout << "Winner!" << endl;
  }   
  system("PAUSE");	
  return 0;
}
Last edited on
Topic archived. No new replies allowed.