Slot Machine

Sep 7, 2009 at 5:39pm
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.
Sep 7, 2009 at 6:16pm
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.
Sep 7, 2009 at 6:42pm
And don't use system("PAUSE"), use std::cin.get();
Sep 7, 2009 at 7:22pm
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 Sep 7, 2009 at 8:02pm
Sep 8, 2009 at 12:20pm
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).






Sep 8, 2009 at 3:47pm
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 Sep 8, 2009 at 4:08pm
Topic archived. No new replies allowed.