# Virtual Slots Implementation

What I can do to make this code more efficient?

It is supposed to be like one of those slot machines where you gamble on it and stuff (like at a casino).

#include <iostream>
#include <conio.h>
#include <string>
#include <ctime>
#include <iomanip>

using namespace std;

int main() {

srand(time(0));

int slot1;
int slot2;
int slot3;
double total_money = 0;
double total_wins = 0;
double total_tries = 0;
double win_percentage;
char play_again;

cout << "Welcomes to Virtual Slots." << endl;  // Welcome the user
cout << "Match 2, win that amount." << endl;  // Explain prizes
cout << "Match 3, win 3x that amount.\n" << endl;

do {

slot1 = rand() % 10 + 1;     // Get random numbers for slots
slot2 = rand() % 10 + 1;
slot3 = rand() % 10 + 1;

cout << "****SLOTS!!****" << endl;
cout << "***************" << endl;
cout << "* [" << slot1 << "] [" << slot2 << "] [" << slot3 << "] *" << endl;  // Show slots
cout << "***************\n" << endl;

if (slot1 == slot2 == slot3) {  // If all three slots match, user getx 3x money

cout << "THREE TIMES MATCH!!" << endl;
cout << "You win $" << (slot1 * 3) << ".\n" << endl; total_money += slot1; // Count money total_wins++; // Count wins } else if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3) { // if user gets 2 slots matched, user gets that amount of money cout << "TWO TIMES MATCH!!" << endl; cout << "You win$" << slot1 << ".\n" << endl;

total_money += slot1;  // Count money
total_wins++;  // Count wins
}

else {  // No match, no money

cout << "NO MATCH.\n" << endl;
}

total_tries++;  // Count tries

win_percentage = (total_wins / total_tries);  // Percentage of wins

cout << "Current Total: $" << setprecision(4) << total_money << endl; // Show user his statistics cout << "Win Percentage: " << setprecision(3) << win_percentage << "%" << endl; cout << "Play Again? (Y/N) " << endl; // Ask the user if s/he wants to play again cin >> play_again; system("PAUSE"); system("CLS"); } while (play_again != 'N'); // Keep going until user says No cout << "See you next time!" << endl; _getch(); return 0; }  • if (slot1 == slot2 == slot3) does not do what you seem to expect. Commented Nov 14, 2014 at 22:23 • @Kawaiiii It compares slot1 and slot2 then compares the resulting bool to slot3. Commented Nov 14, 2014 at 22:27 • So should I do if ((slot1 == slot2) && (slot2 == slot 3))? Commented Nov 14, 2014 at 22:29 • And why does the user win slot1$ if slot2 == slot3 in the second case? Commented Nov 14, 2014 at 22:29
• @Kawaiiii Yes. That would be better. Commented Nov 14, 2014 at 22:30

The usual improvements we often suggest here on Code Review:

## Using namespace:

Try not to using namespace std. Read a discussion about this here. Instead, just explicitly declaring the elements you are using would be better:

using std::cout;
using std::cin;
... etc...


Or don't do any of that and prefix library calls with std::. Not that much more typing anyway.

## system("PAUSE") & friends:

The system() function asks the OS to run whatever program you specify as its argument. It just so happens that on Windows there are two programs in the default path called "PAUSE" and "CLS". This is not portable. On Unix those programs do not exist. But the biggest evil of system() is that it is a major security hole. For example, if a hacker manages to replace "CLS" with an exploit program, your program is going to be the one who fires it. So no serious code outside a learning demo should use those. Be aware of this issue.

## Windows extensions:

_getch() and <conio.h> are Windows extensions. You can replace _getch() with the standard std::cin.get().

## C++11 <random> library:

The new standard has introduced a more powerful pseudo-random number generation library: <random>. This library should be used instead of rand(), srand() for new code.

## Funky if:

This

if (slot1 == slot2 == slot3)


As was noted in the comments, is nonsense. You have to use the && (AND) operator:

if ((slot1 == slot2) && (slot2 == slot3)) {  // If all three slots match, user getx 3x money