Like Tree7Likes

Powerball - Checking Input in Any Order

This is a discussion on Powerball - Checking Input in Any Order within the C++ Programming forums, part of the General Programming Boards category; I need help in trying to get this program to check the results and to tell the user that he ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    4

    Powerball - Checking Input in Any Order

    I need help in trying to get this program to check the results and to tell the user that he or she has won this amount of money because they got the right powerball numbers (in any order they entered them) I kind of starting playing with CheckArray, but I don't know if I'm doing my logic right.

    I'm not asking for someone to do it for me, but to guide me in the right direction. Thanks!

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    #include <conio.h>
    #include <ctime>
    #include "Powerball.h"
    #include <array>
    #include <algorithm>
    #include <functional>
    
    
    using namespace std;
    
    
    char username;
    
    
    const short LOTTO_SIZE = 6;     // Size of arrays for tracking picks
    
    
    // Output shown when the player wins
    const char* const winTxt[] = { 
    "Winner! Jackpot! - $23,000,000",
    "Winner! Five white balls - $1,000,000",
    "Winner! Four white balls & powerball - $10,000!",
    "Winner! Four white balls - $100!",
    "Winner! Three white balls & powerball - $100!",
    "Winner! Three white balls - $7!",
    "Winner! Two white balls & powerball - $7!",
    "Winner! One white ball & powerball - $4!",
    "Winner! Powerball only - $4!"};
    
    
    void header()
    {
        cout << "C++\n"
             << "Powerball: Program 1\n\n";
        cout << "You will be asked to pick a total of six numbers.\n"
             << "The first five numbers must be a value between 1-55.\n"
             << "The last number, the powerball, must be\n"
             << "between 1-42, and may repeat a previously picked value.\n"
             << "All picks must be whole number values.\n\n"
             << "=============================================================\n"
             << "There are nine possible winning combinations:\n"
             << "Five matches + powerball  $23,000,000  Odds 1 in 175,223,510\n"
             << "Five matches              $1,000,000   Odds 1 in 153,632.65\n"
             << "Four matches + powerball  $10,000      Odds 1 in 648,975.96\n"
             << "Four matches              $100         Odds 1 in 19,078.53\n"
             << "Three matches + powerball $100         Odds 1 in 12,244.83\n"
             << "Three matches             $7           Odds 1 in 360.14\n"
             << "Two matches + powerball   $7           Odds 1 in 706.43\n"
             << "One match + powerball     $4           Odds 1 in 110.81\n"
             << "Powerball only            $4           Odds 1 in 55.41\n"
             << "=============================================================\n\n";
    }
    
    
    struct pbSet
    {
        float alone;
        float match1;
        float match2;
        float match3;
        float match4;
        float match5;
    };
    
    
    // Used to keep tally of wins if playing until jackpot
    struct winCount
    {
        pbSet powerball;
        float match3;
        float match4;
        float match5;
    } winnings = {0, 0, 0, 0, 0, 0};    // Counter to track the winnings
    
    
    int menu()
    {
        short temp = 0;
        string username;
        cout << "Enter your name: ";
        cin >> username;
        cout << endl
            << "1. Play\n";
        cout << username + ", pick an menu option: ";
        cin >> temp;
        cout << endl;
        if (!cin)
        {
            cin.clear();
            cin.ignore();
        }
        else
            return temp;
    }
    
    
    void game(const bool& type)
    {
        short *playerPicks,     // numbers chosen by the player
        *randPicks;     // random numbers (winning picks)
        float counter = 1.0f;     // counter of number of tries
        bool win = false;     // tracks win condition
        playerPicks = makePicks(LOTTO_SIZE);
        cout << endl
            << "You've chosen: ";
        for (short i = 0; i < LOTTO_SIZE; i++)
            cout << playerPicks[i] << " ";
        _getch();
        {
            while (!win)
            {
                randPicks = makePicksRand(LOTTO_SIZE);
                cout << "\nTry " << counter << ": ";
                for (short i = 0; i < LOTTO_SIZE; i++)
                    cout << randPicks[i] << " ";
                cout << endl;
                win = checkWin(playerPicks, randPicks, type);
                counter++;
                delete[] randPicks;
            }
        }
    }
    
    
    short* makePicks(const short& size)
    {
        short *temp = new short[size];
        bool repeat = false;
        cout << "Pick your first five numbers.\n" 
             << "Choices must be from 1-55, and may not repeat\n";
        for (short i = 0; i < LOTTO_SIZE;)
        {
            if ((i == 5) && (!repeat))
            {
                cout << "Now, pick your powerball number.\n" 
                    << "Choice must be from 1-42, and may\n"
                    << "repeat any previous pick.\n";
                repeat = true;
            }
            cout << "Pick " << (i + 1) << ": ";
            cin >> temp[i];
            if (!cin)
            {
                cin.clear();
                cin.ignore();
                cout << "Invalid input.\n";
            }
            else
            {
                if (validate(i, temp))
                    i++;
                else
                    cout << "Pick " << (i + 1) << " conflicts with a previous \n"
                         << "choice or is invalid.\n";
            }
        }
        return temp;
    }
    
    
    short* makePicksRand(const short& size)
    {
        short *temp = new short[size];
        for (short i = 0; i < LOTTO_SIZE;)    
        {
            if (i == 5)
                temp[i] = (rand() % 42) + 1;
            else
                temp[i] = (rand() % 55) + 1;
            if (validate(i, temp))
                i++;
        }
        return temp;
    }
    
    
    bool checkArray(int array[5], int size, int number)
    {
        bool has = false;
        for (int i = 0; i< size; i++)
        {
            if (array[i] == number)
            {
                has = true;
            }
        }
        return has;
    }
    
    
    
    
    bool validate(const short& num, const short* picks)
    {
        if (num == 5)    // when checking the last number (powerball)
        {
            if ((picks[num] < 1) || (picks[num] > 42))
                return false;
            else
                return true;
        }
        else     // checks all other numbers
        {
            if ((picks[num] > 55) || (picks[num] < 1))
                return false;
            else if (num > 0)
            for (short i = 0; i <= num; i++)
            if (picks[i] == picks[i + 1])
                return false;
            return true;
        }
    }
    
    
    bool checkWin(const short* player, const short* random, const bool& type)
    {
        bool pbMatch = false;
        short matches = 0;
        for (short i = 0; i < LOTTO_SIZE; i++)
        {
            if (player[i] == random[i])
            {
                if (i == 5)
                    pbMatch = true;
                else
                    matches++;
            }
        }
        if (pbMatch)
            switch (matches)
        {
            case 0:     // $4
                cout << winTxt[8] << endl;
                if (type)
                {
                    winnings.powerball.alone++;
                    return false;
                }
                else
                    return true;
                break;
            case 1:     // $4
                cout << winTxt[7] << endl;
                if (type)
                {
                    winnings.powerball.match1++;
                    return false;
                }
                else
                    return true;
                break;
            case 2:     // $7
                cout << winTxt[6] << endl;
                if (type)
                {
                    winnings.powerball.match2++;
                    return false;
                }
                else
                    return true;
                break;
            case 3:     // $100
                cout << winTxt[4] << endl;
                if (type)
                {
                    winnings.powerball.match3++;
                    return false;
                }
                else
                    return true;
                break;
            case 4:     // $100,000
                cout << winTxt[2] << endl;
                if (type)
                {
                    winnings.powerball.match4++;
                    return false;
                }
                else
                    return true;
                break;
            case 5:     // jackpot
                cout << winTxt[0] << endl;
                return true;
        }
        else
            switch (matches)
        {
            case 3:     // $7
                cout << winTxt[5] << endl;
                if (type)
                {
                    winnings.match3++;
                    return false;
                }
                else
                    return true;
                break;
            case 4:     // $100
                cout << winTxt[3] << endl;
                if (type)
                {
                    winnings.match4++;
                    return false;
                }
                else
                    return true;
                break;
            case 5:     // $1,000,000
                cout << winTxt[1] << endl;
                if (type)
                {
                    winnings.match5++;
                    return false;
                }
                else
                    return true;
                break;
        }
        return false;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The powerball (last) number picked should not be "validated" against the others, as it is allowed to be a duplicate.

    There's no reason to use short instead of int. Always choose int unless you can think of a good reason to use something else. Just because the numbers are small is not a good reason. Saving space is the usual reason (which can also lead to a performance boost for large data structures as more numbers can be kept in the processor cache).

    And it seems a bad idea to employ all those floats when they're being used as plain old ints.

    You should also avoid using a global variable (winnings) for no reason.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    4
    Quote Originally Posted by oogabooga View Post
    The powerball (last) number picked should not be "validated" against the others, as it is allowed to be a duplicate.

    There's no reason to use short instead of int. Always choose int unless you can think of a good reason to use something else. Just because the numbers are small is not a good reason. Saving space is the usual reason (which can also lead to a performance boost for large data structures as more numbers can be kept in the processor cache).

    And it seems a bad idea to employ all those floats when they're being used as plain old ints.

    You should also avoid using a global variable (winnings) for no reason.
    Ok... Thanks for the tip. You have any suggestions for my question?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Sean Kilbane View Post
    Ok... Thanks for the tip. You have any suggestions for my question?
    Try fixing that stuff first.
    Repost your code.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    4
    Powerball needs to be validated as it has to be between 1 and 42.
    Short and int tip I'll keep in mind for my next program. I'm not going to fix that.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Sean Kilbane View Post
    Short and int tip I'll keep in mind for my next program. I'm not going to fix that.
    No problem. I suppose you're keeping the floats, too.
    Well, I'm done here.
    stahta01 likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    4
    That's fine, would rather get help on my actual question then being told to fix something that I'm not worried about at the moment.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,572
    Quote Originally Posted by Sean Kilbane View Post
    That's fine, would rather get help on my actual question then being told to fix something that I'm not worried about at the moment.
    Please read these links
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    How To Ask Questions The Smart Way

    Then ask a smart question.

    The function CheckArray might be perfect; you are NOT using it.
    And, you never stated what you think it should do.

    Tim S.
    Last edited by stahta01; 01-25-2014 at 05:09 PM.
    oogabooga likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,700
    I don't think you asked an actual question. Did you discover a specific problem with this or are we just looking at what you wrote?
    CheckArray() will be an important function because you said that the input can be generally in any order.
    Other than that, I don't know what you want.
    oogabooga likes this.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,258
    That's fine, would rather get help on my actual question then being told to fix something that I'm not worried about at the moment.
    O_o

    I interpret your comment as: "I am going to ignore the help I receive because I know enough to judge what is and is not important.".

    *shrug*

    If you aren't going to try and apply what others are trying to show you, why would anyone else help you?

    Soma
    oogabooga likes this.
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Likes all 'round!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,361
    It seems that this recent post was also from you? (The same name is mentioned.)

    I'll give you points for following some of the advice on that thread - but this looks suspiciously similar to code posted elsewhere almost eight years ago.

    I appreciate that you're trying, but if you really want to learn, you need to figure it out yourself from scratch. That's the only way you're going to succeed.
    Hodor likes this.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,258
    I'll give you points for following some of the advice on that thread - but this looks suspiciously similar to code posted elsewhere almost eight years ago.
    O_o

    Okay, but who was responsible for stripping out some of the comments which are actually useful? Why was that done?

    This went from flaky to kind of bizarre.

    Who wants to write an "SCP" article on "The Powerball Code"?

    Soma

    Edit on behalf of phantomotap:
    "SCP" can have some pretty offensive or disturbing content; it is not some sort of programming website.
    Last edited by laserlight; 01-25-2014 at 09:29 PM.
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by phantomotap View Post
    Who wants to write an "SCP" article on "The Powerball Code"?
    You reference some weird sites!
    phantomotap likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  15. #15
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,258
    ^___^

    Indeed.

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM
  2. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 03:06 AM
  3. input checking
    By kiki in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 03:25 AM
  4. Powerball
    By Theologian in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-24-2001, 07:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21