* Slot Machine Program by Harde*/ 

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{

int x,a, b, c, token=4;
srand(time(0));

cout<<"\t********************************************************\n"
<<"\t* Welcome to slot machine. *\n"
<<"\t* Would you like to play? (1 to play, 2 not to play) *\n"
<<"\t********************************************************\n\n";
cin>>x;
while(token!=0)
{cout<<"You have "<<token<< " tokens\n\n"
<<"Pull? (1 to pull, 2 not to pull)\n\n";
cin>>x;




if(x==1)
{ 

a = 1+rand() %10;
b = 1+rand() %10;
c = 1+rand() %10;
// b = a;
// c = a;

cout<<"\t\t"<<a<<" "<<b<<" "<<c<<"\n\n";

}
else
cout<<"OK\n";
{ 

if(a==b && b==c)
{

token+=4;
cout<<"You win\n\n";
} 
else if(a==b || b==c || a==c)
{
token+=1;

cout<<"You got two out of three\n\n";


}
else
{
token-=1;


cout<<"You lose\n\n";
}

}
}



return 0;
}

I got all this so far !!!

Nex is ..


Copy the program thats on top ^. modify the program to be array based or to use functions or both. Because it is a copy it will have support for writing to an output file. It does not have to work exactly like the original (original is the program on top ^) but it must be playable in a similar manner, ie, it can be an improvement upon the original in whatever way you wish. You should implement a minimum of two arrays or functions.
add several other programming techniquessuch as string handling, switch statements, and so forth.

Recommended Answers

All 11 Replies

Looks like an interesting project. Good luck.

This is what i have done so far without anyones effort.. now plese can you guys help me with the rest a l'll, adding another array, string, switch statement, functions.
Thank YOUUU.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
	
	int x,a, b, c, token=4;
	srand(time(0));
	
	cout<<"\t********************************************************\n"
	    <<"\t*              Welcome to slot machine.                *\n"
		<<"\t*  Would you like to play? (1 to play, 2 not to play)  *\n"
		<<"\t********************************************************\n\n";
	cin>>x;
	while(token!=0)
	{cout<<"You have "<<token<< " tokens\n\n"
	    <<"Pull? (1 to pull, 2 not to pull)\n\n";
	cin>>x;
	
	
	
int intArray[3]; //declare array of ints

 for(int x=0; x<3; x++)  //fill array with random numbers
  {

   intArray[x]=1+rand() %10;
   cout<<intArray[x]<<" "; //print numbers in array

  }
  cout<<endl; 

		
	}
		
				
			return 0;
}

Please guyss neeed some help!

The first step, sadly, is to clean up your code formatting; as it is, it is inconsistent and hard to read.

The next step would be to have the code check for when the user chooses to exit. This can be done simply by testing the user input.

The third step is to actually deduce one token from the payer's tokens every time they play:

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{

    unsigned x, token=4;
    unsigned short playOn;

    srand(time(0));

    cout<<"\t********************************************************\n"
        <<"\t*              Welcome to slot machine.                *\n"
        <<"\t*  Would you like to play? (1 to play, 2 not to play)  *\n"
        <<"\t********************************************************\n\n";
    cin>> playOn;
    while(token!=0)
    {
        cout<<"You have "<<token<< " tokens\n\n"
            <<"Pull? (1 to pull, 2 not to pull)\n\n";
        cin >> playOn;

        // if they no longer want to play, exit the loop
        if (playOn != 1)
            break;

        // deduce one token to pay for the play
        --token;        

        unsigned short wheels[3]; //declare array of ints

        for(int x=0; x<3; x++)  //fill array with random numbers
        {

            wheels[x]=1+rand() % 10;
            cout<<wheels[x]<<" "; //print numbers in array

        }

        cout<<endl;
    }

    return 0;
}

Step three would be to work out how to detect matches and how much the payoff for each ought to be. My recommendation is to have a table which holds all of the winning outcomes, and compare the result with that table. As an example, you might have something like this:

unsigned short payoffTable[][][] = {{10, 10, 10, 1000},
                                    {9, 9, 9, 500},
                                    {8, 8, 8, 200},
                                    {10, 10, 0, 100},
                                    {0, 10, 10, 100},
                                    // etc.
                                   };

(where '0' is the wild card - that is to say, '10, 10, 0, 100' would pay off 100 tokens for a match of two tens in the first two wheels.)

This is just an example of how you can do it; there are any number of approaches you could take.

Thanks Schoil. I appreciate it.

I added string function also writing to an output file option.. can anyone help me telling where should i add arrays in my program (it should display appropraite messages like its doing right now after adding arrays). i have to have an array or function. Thank you

#include <iostream>
#include <ctime>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{

// declare variables
string select = "";

int order;
int x = 0;
int a = 0; // random number 1
int b = 0; // random number 2
int c = 0; // random number 3
int token = 4;
srand(static_cast<int>(time(0)));

// create file object
ofstream outFile;
outFile.open("output.txt", ios::app);

// goes to screen
cout<<"\t********************************************************\n"
	<<"\t* Welcome to slot machine. *\n"
	<<"\t* Would you like to play? (1 to play, 2 not to play) *\n"
	<<"\t********************************************************\n\n";
	cin>>select;
outFile << "\t********************************************************\n"
	<<"\t* Welcome to slot machine. *\n"
	<<"\t* Would you like to play? (1 to play, 2 not to play) *\n"
	<<"\t********************************************************\n\n";

if (select!="2") // means user want to play
	{

	while(token!=0)
	{

	cout<<"You have "<<token<< " tokens\n\n" // goes to the screen
    <<"Pull? (1 to pull, 2 not to pull)\n\n";
	cin>>select;
	outFile<<"You have "<<token<< " tokens\n\n" // goes to  the output file
    <<"Pull? (1 to pull, 2 not to pull)\n\n";





	

if(select!="2") // user does want to play
{ 

	 a = 1+rand() %10;
	 b = 1+rand() %10;
	 c = 1+rand() %10;
//       b = a;
//       c = a;

	
	cout<<"\t\t"<<a<<"          "<<b<<"          "<<c<<"\n\n";
	
	
	
}

else  // user doesn't want to play
	{
		cout<<"Bye Bye\n";
	outFile<<"Bye Bye\n";
	break;
}
	


if(a==b && b==c)  // if all the numbers are same
{

	token+=4;
	cout<<"You win!\n\n"; // to screen
	outFile<<"You win!\n\n"; // to file

} 
else if(a==b || b==c || a==c) // if two numbers are same
{

	token+=1; 

	cout<<"You got two out of three\n\n"; // to screen 
	outFile<<"You got two out of three\n\n"; // to file
}


else
{
	token-=1; // if no numbers are the same 
	cout<<"You lose\n\n"; // to screen 
	outFile<<"You lose\n\n"; // to file
}



}
	} 
	 else 
	cout <<"Bye\n\n";
	outFile <<"Bye\n\n";
	

	// file close
	outFile.close();

system("pause");
return 0;
} // end main

You're kidding, right? You want to now retroactively add an array? That's like deciding after your house is built that you'd really like a basement...

It should have been thought about in the original design before starting the program.

No WaltP. The program on very top was given to us by our teacher and assignment is to add arrays or functions to it (it have to have either one), also other techniques like string. i added string already. MY question is where can i add arrays, after adding arrays the program should display appropriate message like its dong righ now.

Thanks.

Okay guys.. how this looks, can someone check it for me please.
Check list- do i have
string
atleast 2 functions

#include <iostream>
#include <ctime>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
//function prototype
int getRandomnumber();


string select = "";

int a = 0; // random number 1
int b = 0; // random number 2
int c = 0; // random number 3
int token = 4; // no of tokens
srand(static_cast<int>(time(0)));

// create file object
ofstream outFile;
outFile.open("output.txt", ios::app);

// goes to screen
cout<<"\t********************************************************\n"
	<<"\t* Welcome to slot machine. *\n"
	<<"\t* Would you like to play? (1 to play, 2 not to play) *\n"
	<<"\t********************************************************\n\n";
	cin>>select;
outFile << "\t********************************************************\n"
	<<"\t* Welcome to slot machine. *\n"
	<<"\t* Would you like to play? (1 to play, 2 not to play) *\n"
	<<"\t********************************************************\n\n";

if (select!="2") // means user want to play
	{

	while(token!=0)
	{

	cout<<"You have "<<token<< " tokens\n\n" // goes to the screen
    <<"Pull? (1 to pull, 2 not to pull)\n\n";
	cin>>select;
	outFile<<"You have "<<token<< " tokens\n\n" // goes to  the output file
    <<"Pull? (1 to pull, 2 not to pull)\n\n";


if(select!="2") // user does want to play
	{ 
		
	 a = getRandomnumber();
	 b = getRandomnumber();
	 c = getRandomnumber(); //       b = a; //       c = a;
	
	cout<<"\t\t"<<a<<"          "<<b<<"          "<<c<<"\n\n";
	
	 }
	 
	 
else  // user doesn't want to play
	{
		cout<<"Bye Bye\n";
	outFile<<"Bye Bye\n";
	break;
	}
	

if(a==b && b==c)  // if all the numbers are same
	{

	token+=4;
	cout<<"You win!!!\n\n"; // to screen
	outFile<<"You win!!!\n\n"; // to file

	} 
else if(a==b || b==c || a==c) // if two numbers are same
	{

	token+=1; 

	cout<<"You got two out of three\n\n"; // to screen 
	outFile<<"You got two out of three\n\n"; // to file
	}


else
	{
	token-=1; // if no numbers are the same 
	cout<<"You lose\n\n"; // to screen 
	outFile<<"You lose\n\n"; // to file
	}
	}
	} 
	 else 
	cout <<"Bye\n\n";
	outFile <<"Bye\n\n";
	

	// file close
	outFile.close();

system("pause");
return 0;
} // end main




//*************function definitions***************
 int getRandomnumber()
 {
 int randInteger = 0;
 //generate random integer 
 randInteger = 1+rand() %10;
 return randInteger;
 } //end of getRandomNumber function

Any idea about how can i use Switch statment or any other technique in my program above ^ without removing anything.
This be the last time in asking anything about this program. i promise.

Thank you.

No WaltP. The program on very top was given to us by our teacher and assignment is to add arrays or functions to it (it have to have either one), also other techniques like string. i added string already. MY question is where can i add arrays, after adding arrays the program should display appropriate message like its dong righ now.

Thanks.

You didn't say that before.

Well, the way you add arrays is look carefully at the program and find a group of variables that together work quite similarly. Is there a way to convert those variables into one array?

Is there a a series of values you do the same work on, replicating the code over and over? This points to a possibility where a loop and array can be used once instead of multiple times.

In this program, a,b,c seem to be excellent contenders for an array. And your IFs can be converted to 1 or 2 loops.

You didn't say that before.

Well, the way you add arrays is look carefully at the program and find a group of variables that together work quite similarly. Is there a way to convert those variables into one array?

Is there a a series of values you do the same work on, replicating the code over and over? This points to a possibility where a loop and array can be used once instead of multiple times.

In this program, a,b,c seem to be excellent contenders for an array. And your IFs can be converted to 1 or 2 loops.

Hi WaltP,
The program at bottom.. i consider it my final program for my homework assignment, If you can please please review it for me.

Assignemet was adding atleast two arrays or function, also some techiques like switch statement, string etc.. to the program which is in my question (on page 1).

Hope to hear from you soon caz its due tonight.

#include <iostream>
#include <ctime>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
	//function prototype
	int getRandomnumber();

	string choose = ""; // string use
	int select;
	int a = 0; // random number 1
	int b = 0; // random number 2
	int c = 0; // random number 3
	int token = 4; // no of tokens
	srand(static_cast<int>(time(0))); // function

	// create file object
	ofstream outFile;
	outFile.open("output.txt", ios::app);

	// goes to screen
	cout<<"\t********************************************************\n"
	<<"\t* Welcome to slot machine. *\n"
	<<"\t* Would you like to play? (1 to play, 2 not to play) *\n"
	<<"\t********************************************************\n\n";
	cin>>choose;
	outFile << "\t********************************************************\n"
	<<"\t* Welcome to slot machine. *\n"
	<<"\t* Would you like to play? (1 to play, 2 not to play) *\n"
	<<"\t********************************************************\n\n";

	while (choose!="2") // means user want to play
	{
		
		while(token!=0)
		{

		cout<<"You have "<<token<< " tokens\n\n" // goes to the screen
		<<"Pull? (1 to pull, 2 not to pull)\n\n";
		cin>>select;
		outFile<<"You have "<<token<< " tokens\n\n" // goes to the output file
		<<"Pull? (1 to pull, 2 not to pull)\n\n";

			switch (select)
			{

				case 1:

					a = getRandomnumber();
					b = getRandomnumber();
					c = getRandomnumber(); // b = a; // c = a;

					cout<<"\t\t"<<a<<"       "<<b<<"       "<<c<<"\n\n";
					
					if (a==b && b==c && b==c) // if all the numbers are same
					{

					token+=4;
					cout<<"You win!!!\n\n"; // to screen
					outFile<<"You win!!!\n\n"; // to file
					} 

					else if (a==b || b==c || a==c) // if two numbers are same
					{

					token+=1; 

					cout<<"You got two out of three\n\n"; // to screen 
					outFile<<"You got two out of three\n\n"; // to file
					}


					else 
					{
					token-=1; // if no numbers are the same 
					cout<<"You lose\n\n"; // to screen 
					outFile<<"You lose\n\n"; // to file
					}
					break;
		

				case 2: // user doesn't want to play
	
					cout<<"OK\n";
					outFile<<"OK\n";
					break;
			}

		}
	} 
	
	cout <<"Good Bye\n\n";
	outFile <<"Good Bye\n\n";
	

	// file close
	outFile.close();

	system("pause");
	return 0;
}	// end main




//*************function definitions***************
	int getRandomnumber()
{
	int randInteger = 0;
	//generate random integer 
	randInteger = 1+rand() %10;
	return randInteger;
}	//end of getRandomNumber function
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.