Thread: Powerball - Checking Input in Any Order

1. 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;

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!"};

{
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

{
short temp = 0;
cout << "Enter your name: ";
cout << endl
<< "1. Play\n";
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. 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.

3. Originally Posted by oogabooga
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. Originally Posted by Sean Kilbane
Ok... Thanks for the tip. You have any suggestions for my question?
Try fixing that stuff first.

5. 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. Originally Posted by Sean Kilbane
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.

7. 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. Originally Posted by Sean Kilbane
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.
What Every Computer Scientist Should Know About Floating-Point Arithmetic
How To Ask Questions The Smart Way

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

Tim S.

9. 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.

10. 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

11. Likes all 'round!

12. 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.

13. 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.

14. Originally Posted by phantomotap
Who wants to write an "SCP" article on "The Powerball Code"?
You reference some weird sites!

15. ^___^

Indeed.

Soma