Thread: Slot Machine Problem

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Slot Machine Problem

    Slot Machine Problem

    Hey there! I have begun to write a slot machine program/game. I have got it to work and the code looks like this:

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <string>
    using namespace std;
    int main()
    {
        srand(time(0));
        int again;
        int number1 = rand() % 3 + 1;
        int number2 = rand() % 3 + 1;
        int number3 = rand() % 3 + 1;
        cout << number1 << number2 << number3 << '\n' << endl;
        if (number1 != 3 || number2 != 3 || number3 != 2)
        {
            cout << "You lose!\n";
            cout << "Try Again?\n";
            cin >> again;
        }
        else
        {
            cout << "You won!\n";
        }
        if (again == 1)
        {
            cout << main();
        }
        if (again == 2)
        {
            EXIT_SUCCESS; 
        }
    }
    But the problem is that I don't know how to have more than 1 number that's correct. Right now have I the number 332 but I want more numbers that's correct. At least 3 numbers. I tried to code the same lines but with another numbers in it but that didn't worked. Also coded I this line once more with another number:
    Code:
        if (number1 != 2 || number2 != 3 || number3 != 3)
        {
            cout << "You lose!\n";
            cout << "Try Again?\n";
            cin >> again;
        }
        else
        {
            cout << "You won!\n";
        }
    Please can someone tell me how to do that?.

  2. #2
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    It took me a while to figure out your definition of 'slot machine' and therefore understand the problem. I'm guessing there's only 1 number that's correct (332) and you are supposed to (randomly) generate those exacly numbers in order to win, am I right?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I think you need to recheck your logic.
    Code:
    if (number1 != 2 || number2 != 3 || number3 != 3)
    This statement will evaluate to true if any one of the checks evaluate to true. You only want it to execute if all of the checks evaluate to true.

    Jim

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I made one myself and it worked out for 2 numbers like this.

    Code:
    #include <iostream>#include <ctime>
    #include <cstdlib>
    #include <string>
    using namespace std;
    int main()
    {
        srand(time(0));
        int again;
        int number1 = rand() % 3 + 1;
        int number2 = rand() % 3 + 1;
        int number3 = rand() % 3 + 1;
        cout << number1 << number2 << number3 << '\n' << endl;
        if ((number1 == 3 && number2 == 3 && number3 == 2) || (number1 == 3 && number2 == 3 && number3 == 3))
        {
            cout << "You won!\n";
        }
        else
        {
            cout << "You lose\n";
        }
    }
    This works well for 2 different numbers, I guess you just have to add a 3rd one to the code and voila.
    Last edited by Khabz; 03-02-2013 at 12:49 PM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Never call the main function!
    The main function is NOT supposed to be recursive!

    Code:
    cout << main();
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Just store your numbers in a list somewhere, then you can have as many as you want:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    namespace {
        int hit[][3] = {
            {1, 2, 3},
            {2, 2, 3},
            {3, 2, 3},
        };
    }
    
    int main()
    {
        std::string buf;
    
        do {
            int a = 1 + rand() % 3;
            int b = 1 + rand() % 3;
            int c = 1 + rand() % 3;
    
            std::cout << a << ' ' << b << ' ' << c << std::endl;
    
            // Check for a hit
            for (std::size_t i = 0; i < sizeof hit / sizeof *hit; i++) {
                if (a == hit[i][0] && b == hit[i][1] && c == hit[i][2]) {
                    std::cout << "Winner!" << std::endl;
                }
            }
    
            std::cout << "Again? (EOF to quit): ";
        } while (getline(std::cin, buf));
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Khabz thanks! Your code where very clear to read for me for I am a beginner with C++ so that helped me very much thank you!.
    Prelude I haven't got so far with C++ yet so I didn't understand all of your code. But I tested your code and it worked well!.
    stahta01 if it is a dumb idea to call the main() function what can I call instead of main()?
    Last edited by DecoratorFawn82; 03-02-2013 at 03:09 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by DecoratorFawn82 View Post
    stahta01 if it is a dumb idea to call the main() function what can I call instead of main()?
    Nothing. Calling main() is the worst way to restart the program. Instead you should use a loop like Prelude did to restart the slot machine. It would be even better if playing the slot machine was its own function, then you can just call that in a loop.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Calling main() is the worst way to restart the program.
    Mostly because it's technically illegal in C++. Some compilers support it as a historical artifact, but even in C (the language that C++ inherited that feature from) it's exceedingly poor style.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c programm slot machine change
    By schugh in forum C Programming
    Replies: 2
    Last Post: 05-17-2011, 05:23 PM
  2. Slot Machine program c++ bug
    By nobo707 in forum C++ Programming
    Replies: 12
    Last Post: 09-23-2009, 07:29 PM
  3. Replies: 4
    Last Post: 01-18-2008, 07:05 PM
  4. Slot Machine
    By nomi in forum C Programming
    Replies: 9
    Last Post: 01-03-2004, 10:13 AM
  5. IDEA: A Slot Machine (aka a fruit machine)
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:13 PM