Slot Machine (Jumping into c++)

Hello there c++ programmers.

I have a small, small problem. Yet it irritates me slightly. As I am interested in picking up everything I can learn, since (as far as I understand) c++ is really hard, and it requires you to know alot.

Anyways here is the problem.

The Slot Machine I wrote, generates 3 random numbers with the possibility of going from (1 - 3). However, after a do- while statement that asks if you want to try again. It just generates the same numbers it did in the start. Over and over again.

For example: If I run it, it may generate: 1 || 2 || 3. And then it propmts the user to try again. If he types "Y" it restarts. However, when he spin the slot machine again, it generates the same numbers ( 1 || 2 || 3). I need to exit the console and then open it up again for it to generate 3 new random numbers between 1 and 3.

EDIT: I forgot to ask if it was possible to make the console generate 3 NEW random numbers after the user responded with 'Y'.


Source code:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

int randRange(int low, int high);

int main(){
srand(time(NULL));
int num1 = randRange(1, 3);
int num2 = randRange(1, 3);
int num3 = randRange(1, 3);
string spin;
string again;

cout << "                    # SLOT MACHINE #" << endl << endl << endl;
do{
cout << "         Type spin, to spin the SLOT MACHINE!" << endl << endl;
cin >> spin;
if(spin == "spin" || spin == "SPIN"){

	cout << "                    " << num1 << " || " << num2 << " || " << num3 << endl << endl;
		
		if(num1 + num2 + num3 == 3 || num1 + num2 + num3 == 6 || num1 + num2 + num3 == 9){

			cout << "                      JACKPOT" << endl << endl;
		}
}

else{

	cout << "You didn't spin the SLOT MACHINE" << endl << "TRY AGAIN" << endl << endl;

}

cout << "Wanna spin again? < Y || N >: ";
	cin >> again;
}while( again == "y" || again == "Y");

cout << endl << "Thankyou for playing" << endl << endl;


system("pause");
return 0;
}

int randRange(int low, int high){

	return rand() % (low - high) + low;
}
Last edited on
Ha, that's funny I learned using that book too.

You never called 'randRange' one it restarted.

By the way for Want to spin again use a char instead of a std::string it (however soooo small) saves memory.
Yeah, it's a good book hehe ^^.

Anyways thanks, it worked!

When I try to change 'string again' to a char.

It pops up with the error:
error C2446: '==' : no conversion from 'const char *' to 'int'
Topic archived. No new replies allowed.