Hello everyone. I need some help on creating a slot machine. It needs to have the option of playing a three wheel & four wheel machine. I've created separate functions for those two. I think I understand the whole random generating concept, but I'm having a hard time figuring out how to get the generator to randomize words ("bar," "cherry," "seven," etc.) instead of numbers. I know this isn't perfect, since it's far from finished. But if I can get a nice little push in the right direction, and any suggestions on what I could do differently (in anything I've done), I'd appreciate it. Currently not getting any errors, so that's a plus!

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

using namespace std;

int credits = 0;
int bet = 0;
int winnings = 0;

string chr1;
string chr2;
string chr3;
string chr4;

void menu(char decision);
void earnings();
void three(); // for three wheel slot machine.
void four(); // for four wheel slot machine.

int main()
{
    char pay = 0;
    char decision;

    cout << "FANCY NAME SLOT MACHINE!\n\n";
    cout << "'3' - start the three wheel slot machine.\n'4' - start the four wheel slot machine.\n"
            "'T' - access the pay table.\n'B' - place your bet(s).\n"
            "'S' - press your luck, spin the wheel\n'P' - collect your earnings & cash out.\n"
            "'Q' - quit the game.\n" << endl;

    cout << "Let's play! What would you like to do? ";
    cin >> decision;

    menu(decision);

    return 0;

}

void menu(char decision)
{

    if (decision == 'T')
    {
        cout << "THREE WHEEL SLOT MACHINE:    FOUR WHEEL SLOT MACHINE:            PAYOUT:\n";
        cout << "STAR - STAR - STAR           STAR - STAR - STAR - STAR           1,000 COINS\n";
        cout << "BAR - BAR - BAR              BAR - BAR - BAR - BAR               500 COINS\n";
        cout << "SEVEN - SEVEN - SEVEN        SEVEN - SEVEN - SEVEN - SEVEN       250 COINS\n";
        cout << "BAR - BAR - ANY              BAR - BAR - BAR - ANY               100 COINS\n";
        cout << "PLUM - PLUM - PLUM           PLUM - PLUM - PLUM - PLUM           75 COINS\n";
        cout << "LEMON - LEMON - LEMON        LEMON - LEMON - LEMON - LEMON       50 COINS\n";
        cout << "CHERRY - CHERRY - ANY        CHERRY - CHERRY - ANY - ANY         25 COINS\n";
        cout << "CHERRY - ANY - ANY           CHERRY - ANY - ANY - ANY            5 COINS\n" << endl;
    }

    if (decision == '3')
    {
        three();
    }

    if (decision == 'P')
    {
        if (credits > 0)
        {
            cout << "You've cashed out " << credits << "!" << endl;
            credits = 0;
        }

    menu(decision);
    }
}

void three()
{
    int j = 0;
    cout << "Enter your bet (CTRL + C to quit): ";
    cin >> bet;

   for (int i = 0; i < 3; i++)
    {
       (j = (rand() % 6) + 1);
    }

    if (chr1 == "STAR" && chr2 == "STAR" && chr3 == "STAR")
    {
        cout << "JACKPOT!\n";
        winnings = bet * 1000;
    }
        cout << "You won " << winnings << endl;

        cout << "Enter your bet (CTRL + C to quit): ";
        cin >> bet;

    if (chr1 == "BAR" && chr2 == "BAR" && chr3 == "BAR")
    {
        cout << "BAR x3\n";
        winnings = bet * 500;
    }
        cout << "You won " << winnings << endl;

        cout << "Enter your bet (CTRL + C to quit): ";
        cin >> bet;

    if (chr1 == "SEVEN" && chr2 == "SEVEN" && chr3 == "SEVEN")
    {
        cout << "SEVEN x3\n";
        winnings = bet * 250;
    }
        cout << "You won " << winnings << endl;

        cout << "Enter your bet (CTRL + C to quit): ";
        cin >> bet;

    if (chr1 == "BAR" && chr2 == "BAR" && chr3 == "ANY")
    {
        cout << "BAR x2\n";
        winnings = bet * 100;
    }
        cout << "You won " << winnings << endl;

        cout << "Enter your bet (CTRL + C to quit): ";
        cin >> bet;

        if (chr1 == "PLUM" && chr2 == "PLUM" && chr3 == "PLUM")
    {
        cout << "PLUM x3\n";
        winnings = bet * 75;
    }
        cout << "You won " << winnings << endl;

        cout << "Enter your bet (CTRL + C to quit): ";
        cin >> bet;

        if (chr1 == "LEMON" && chr2 == "LEMON" && chr3 == "LEMON")
    {
        cout << "LEMON x3\n";
        winnings = bet * 50;
    }
        cout << "You won " << winnings << endl;

        cout << "Enter your bet (CTRL + C to quit): ";
            cin >> bet;

        if (chr1 == "CHERRY" && chr2 == "CHERRY" && chr3 == "ANY")
    {
        cout << "CHERRY x2\n";
        winnings = bet * 25;
    }
        cout << "You won " << winnings << endl;

        cout << "Enter your bet (CTRL + C to quit): ";
            cin >> bet;

        if (chr1 == "CHERRY" && chr2 == "ANY" && chr3 == "ANY")
    {
        cout << "Very nice!";
        winnings = bet * 5;
    }
        cout << "You won " << winnings << endl;
}

You could create an array or vector that holds each of the 6 words. Then the random number you generate is used as an index into that array to display the appropriate word.

Also, using those three random numbers will allow you to shorten your comparison tests for winning combinations, at the expense of a bit of clarity.

Consider also using and enuneration of the words.

Just another point. This bit of code in the void three( )

for (int i = 0; i < 3; i++)
{
  (j = (rand() % 6) + 1);
}

Isn't doing a whole lot of good. Three random numbers get generated, but only the last one is stored and available for further use. Maybe you need something more like:

   w1 = (rand() % 6) + 1);
   w2 = (rand() % 6) + 1);
   w3 = (rand() % 6) + 1);

Would I need to create an enum for each wheel? Like, the three wheel machine needs three wheels (obviously), so would I need something like:

enum wheel1 {star = 1, bar, seven, plum, lemon, cherry};

Or:

const string wheel1[6] =
{
"STAR",
"BAR",
"SEVEN",
"PLUM",
"LEMON",
"CHERRY"
};

And do it three times (four times for the four wheel machine)? I've actually tried those two options with absolutely no progress. Before trying those, I had everything in a case statement, but it kept printing out the same response each time. Absolutely no random-ness at all.

switch (wheel1)
    {
    case 1:
        cout << "STAR" << " ";
        break;
    case 2:
        cout << "BAR" << " ";
        break;
    case 3:
        cout << "SEVEN" << " ";
        break;
    case 4:
        cout << "PLUM" << " ";
        break;
    case 5:
        cout << "LEMON" << " ";
        break;
    case 6:
        cout << "CHERRY" << " ";
        break;
    }

With each wheel in its own switch statement.

I did change the random-ness code to what you suggested and that was able to give me output, but like I said, it just gave me the same output each time I ran the program. I can get the stupid thing to run if I use just numbers and not numbers converted to words.

I got the most progress with switch statements, but I just don't know what I'm doing wrong as far as getting them to generate randomly. I'll post what I currently have, if you want to have a look.

I know it's not right, far from it, but I'm at a loss.

void three()
{
    int wheel1, wheel2, wheel3;

    wheel1 = (rand() % 6) + 1;
    wheel2 = (rand() % 6) + 1;
    wheel3 = (rand() % 6) + 1;

    cout << "You currently have " << credits << " credits." << endl;
    cout << "How many of those credits would you like to bet? ";
    cin >> bet;
    cout << endl;

    // switch statement for the first wheel
    switch (wheel1)
    {
    case 1:
        cout << "STAR -" << " ";
        break;
    case 2:
        cout << "BAR -" << " ";
        break;
    case 3:
        cout << "SEVEN -" << " ";
        break;
    case 4:
        cout << "PLUM -" << " ";
        break;
    case 5:
        cout << "LEMON -" << " ";
        break;
    case 6:
        cout << "CHERRY -" << " ";
        break;
    }

    // switch statement for the second wheel

    switch (wheel2)
    {
    case 1:
        cout << "STAR -" << " ";
        break;
    case 2:
        cout << "BAR -" << " ";
        break;
    case 3:
        cout << "SEVEN -" << " ";
        break;
    case 4:
        cout << "PLUM -" << " ";
        break;
    case 5:
        cout << "LEMON -" << " ";
        break;
    case 6:
        cout << "CHERRY -" << " ";
        break;
    }

    // switch statement for wheel three
    switch (wheel3)
    {
    case 1:
        cout << "STAR -" << " ";
        break;
    case 2:
        cout << "BAR -" << " ";
        break;
    case 3:
        cout << "SEVEN -" << " ";
        break;
    case 4:
        cout << "PLUM -" << " ";
        break;
    case 5:
        cout << "LEMON -" << " ";
        break;
    case 6:
        cout << "CHERRY -" << " ";
        break;
    }

    if (wheel1 == 1 && wheel2 == 1 && wheel3 == 1) // THREE STARS (1,000)
    {
        credits = credits - bet;
        cout << "JACKPOT! You won 1,000 credits." << endl;
        cout << "You currently have " << credits + 1000 << " credits." << endl;
    }
    else if (wheel1 == 2 && wheel2 == 2 && wheel3 == 2) // THREE BARS (500)
    {
        credits = credits - bet;
        cout << "AMAZING! You won 500 credits." << endl;
        cout << "You currently have " << credits + 500 << " credits." << endl;
    }
    else if (wheel1 == 2 && wheel2 == 2 && wheel3 == 2) // THREE SEVENS (250)
    {
        credits = credits - bet;
        cout << "AWESOME! You won 250 credits." << endl;
        cout << "You currently have " << credits + 250 << " credits." << endl;
    }
    else if (wheel1 == 2 && wheel2 == 2 && wheel3 == 0) // TWO BARS & ANYTHING (100) - How do I get it to random one of the others?!
    {
        credits = credits - bet;
        cout << "NICE! You won 100 credits." << endl;
        cout << "You currently have " << credits + 100 << " credits." << endl;
    }
    else if (wheel1 == 4 && wheel2 == 4 && wheel3 == 4) // THREE PLUMS (75)
    {
        credits = credits - bet;
        cout << "GOOD JOB! You won 75 credits." << endl;
        cout << "You currently have " << credits + 75 << " credits." << endl;
    }
    else if (wheel1 == 5 && wheel2 == 5 && wheel3 == 5) // THREE LEMONS (50)
    {
        credits = credits - bet;
        cout << "GOOD JOB! You won 50 credits." << endl;
        cout << "You currently have " << credits + 50 << " credits." << endl;
    }
    else if (wheel1 == 6 && wheel2 == 6 && wheel3 == 0) // TWO CHERRIES & ANYTHING (25)
    {
        credits = credits - bet;
        cout << "GOOD JOB! You won 25 credits." << endl;
        cout << "You currently have " << credits + 25 << " credits." << endl;
    }
    else if (wheel1 == 6 && wheel2 == 0 && wheel3 == 0) // ONE CHERRY & TWO ANYTHING (5)
    {
        credits = credits - bet;
        cout << "GOOD JOB! You won 5 credits." << endl;
        cout << "You currently have " << credits + 5 << " credits." << endl;
    }
}

I don't have time at this moment to dig into the whole thing, but the fact that you get the same result every time you run the program is because you are not seeding the randomizer. In main( ), add:
srand( unsigned(time(NULL)));

You should generally work on a program in stages - heroic programming (writing the whole thing at once and hoping for the best) seldom works. Get one thing working, then move on to the next building on that.

So, here's what may serve as a foundation for you.

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

using namespace std;

void three ( );

int main(void)
{
    srand( unsigned( time( NULL ) ) );

    for( int i = 0; i < 5; i++ )
        three( );

    return 0;
}


void three()
{
    const string labels[6] =
    {
        "STAR",
        "BAR",
        "SEVEN",
        "PLUM",
        "LEMON",
        "CHERRY"
    };
    int wheels[3] = { 0 };


    //spin the wheels   
    for( int i = 0; i < 3; i++ )
        wheels[i] = rand( ) % 6; //no +1, using as indexes


    // display wheels
    for( int i=0; i < 3; i++ )
        cout << labels[wheels[i]] << " ";

    cout << endl << endl;

}

You should be able to modify this to handle any number of wheels, simply by passing in a paramter of how many wheels to use in any invocation.

You should be able to simply the scoring portion, again using loops to examine the contents of the wheels array.

Thank you so much for your help thus far! I might have further questions, since there are a few things I am still uncertain on how I'm going to do it (because I haven't even really started it yet). I am prone of doing the "heroic programming," and it's been getting me into a bit of trouble. My professor is always telling me I'm not thinking things out in the grander scheme of things, so I guess I over compensate by trying to do everything at once.

Just wondering, though, since I've never seen it before.

int wheels[3] = { 0 };

What does the { 0 } represent?

OK, one more thing (sorry!) -

There's the payout of 100 if you get a 2x bar and anything else (in any order), how do I evaluate the "any" part? For three of a kind, I'm taking the array value...

I'm currently using this:

if (labels[wheels[1]] == 1 && labels[wheels[2]] == 1 && labels[wheels[3]] == 1)
    {
        credits = credits - bet;
        cout << "JACKPOT! You won 1,000 credits." << endl;
        cout << "You currently have " << credits + 1000 << " credits." << endl;
    }

for the star - star - star payout, but getting the error of "no operator '==' matches these operands."

int wheels[3] = { 0 };The { 0 } is the initializer list for the array. By setting the first element to 0, the compiler will intialize all the remaining elements to zero. That's just to ensure the array is at some known state before proceeding.

For scoring, you could take what you've got an continue in that vein, but I think it will get long and complicated before you're done.

But, in the pattern you have, you need to make good use of if...else if... structures.

if (labels[wheels[1]] == 1 && labels[wheels[2]] == 1 && labels[wheels[3]] == 1)
{
        credits = credits - bet;
        cout << "JACKPOT! You won 1,000 credits." << endl;
        cout << "You currently have " << credits + 1000 << " credits." << endl;
}

else if (labels[wheels[1]] == 1 && labels[wheels[2]] == 1 ||
        labels[wheels[1]] == 1 && labels[wheels[3]] == 1 || 
        labels[wheels[2]] == 1 && labels[wheels[3]] == 1 )
{
        credits = credits - bet;
        cout << "You won 100 credits." << endl;
        cout << "You currently have " << credits + 100 << " credits." << endl;
}

So, this tests first for the most restrictive possibility, then a lesser version.

From your question above, it seems you might be a bit weak in the area of dealing with arrays to represent concepts as opposed to simply storing collections of values.

If I was writing this program, I'd have a six element array of integers, use the values in the wheels array as indexex into that scoring array, incrementing the for each time a value occurs in the wheels. So you'll have six elements to inspect, they can have values 0,1,2,or 3. If you find a 3 at the index that represents Cherry - you have your scoring information. A lot less writing than the long bunch of if...else if... you setting up now.

But that's just me. There are several approaches one can take.

Nail on the head. We've covered arrays in the sense of storing collections of values but never actually using them for much of anything. So, yes, I am actually staring at this with the deer in the headlights look. I'm staring at it trying to understand it, though, so... give me some credit! :)

I was looking around on some of the older threads on some of the same problems and saw another post that you've commented on. I've since misplaced the link, but I recall it going into detail of what you just described. I couldn't make sense of that either.

I can't test my if/else/else if at the moment, because it's still giving me the error

no operator "==" matches these operands.

Which caused me to believe I was doing something immensely wrong. (And I needed a break anyway.)

I changed this coding around so much since originally posting, but I just wanted to thank you vmanes! I took so many different routes that weren't working, and I had originally started with what you suggested of assigning the array to the string (string to the array?)

labels[wheel1] . . . 

I couldn't get it working right at first, so I attempted other techniques but couldn't get certain aspects to work. I eventually went back to this way, and changing the names of the string and array helped me understand a little more of what I was needing to do.

Then everything else started falling into place and I just had to THINK about it, since it was a bunch of stuff we had already covered.

But thank you, vmanes. Lifesaver.

I was wondering if you'd be back. Glad you're making progress.

Now when I make an assignment like this for my students next term, they'll have you to thank for the inspiration!

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.