2

I am trying to write a simulator for playing the Powerball lottery, where the program would ask for 5 numbers (aka the white balls) and be inputted into a 6 element array and another number (the red Powerball) into the 6th element. I need to figure out how to test for duplicates in the first 5 elements but the 6th doesn't need to be unique.

I have a loop that I thought would work but it doesn't even execute and is rather messy.

Is there a more efficient way to test for duplicates, maybe involving a bool flag?

const int PBALLAMOUNT = 6;
const int PBMAX = 69;
const int PBMIN = 1;
const int REDMAX = 26;

cout << "Enter the numbers you want to use for the white powerballs" << endl;
for (int k = 0; k < PBALLAMOUNT - 1; k++)
{
    cin >> pBallNums[k];
    while (pBallNums[k] < PBMIN || pBallNums[k]>PBMAX) 
    {
        cout << "Invalid input! Please enter different numbers between 1 and 69" << endl;
        cin >> pBallNums[k];
    }
}

bool dup = false;
for (int i = 0; i < PBALLAMOUNT - 1; i++) 
{
    for (int j = i + 1; j < PBALLAMOUNT - 1; j++) 
    {
        while (!dup) 
        {
            if (pBallNums[i] == pBallNums[j]) 
            {
                cout << "Please enter a unique number from the others in your PowerBall number selection" << endl;
                cin >> pBallNums[i];
            }

        }
    }
}

cout << "And what would you like for your redball" << endl;
cin >> pBallNums[5];

while (pBallNums[5] < PBMIN || pBallNums[5] > REDMAX)
{
    cout << " The red powerball needs to be between 1 and 26: ";
    cin >> pBallNums[5];
}

I basically just need it to alert the user if they have already entered a number into the array and offer another std::cin >> pBallNums statement but the actual result is just nothing happens after you enter the numbers.

6
  • What is pBallNums? An array or an std::vector? And does the order of the values in pBallNums matter? Commented May 7, 2019 at 19:29
  • 1
    std::any_of (and friends) seem like an obvious tool to reach for.. Commented May 7, 2019 at 19:31
  • 4
    Consider using a std::set. All items in the set are guaranteed unique. Commented May 7, 2019 at 19:32
  • 1
    Surprised by the upvotage the set comment received. For a list of at most size 4 @JesperJuhl 's pitch is going to be much more efficient Commented May 7, 2019 at 20:20
  • 1
    @JeJo Neat site. Going to have to play with that later. All I see is that both execute too fast for the benchmark. For a few entries run once that's to be expected. Commented May 8, 2019 at 15:14

3 Answers 3

3

"I basically just need it to alert the user if they've already entered a number into the array and offer another cin >> pBallNums statement."

In that case simply use std::set and use it's std::set::emplace method to store the user input into the set.

From cppreference.com,

template< class... Args >
std::pair<iterator,bool> emplace( Args&&... args );

std::set::emplace

Returns a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a bool denoting whether the insertion took place. True for Insertion, False for No Insertion.

Simply take this information for your case and loop again for the next user input.


Here is a sample code (See Live):

#include <iostream> 
#include <set>

int main()
{
    std::set<int> mySet;

    for(int loopCount{ 0 }; loopCount != 5; )// repeat until maximum loop count
    {
        int input; std::cin >> input; // user input
        // get the std::pair<iterator,bool>
        const auto pairIter = mySet.emplace(input);
        // increment maximum loop count, if insertion successful
        if (pairIter.second) ++loopCount;
    }
    for (const int userInput : mySet)
        std::cout << userInput << " ";

    return 0;
}

Sample input:

1
1
2
3
4
2
4
3
5

Output:

1 2 3 4 5
Sign up to request clarification or add additional context in comments.

Comments

1

Sort, then check adjacent items for equality.

Comments

1

First, try not to mix real requirements with implementation details.

[...] where the program would ask for 5 numbers (aka the white balls) and be inputted into a 6 element array and another number (the red powerball) into the 6th element.

What you really want is: 5 distinct numbers taken from user input. And from your code I read that the check should happen after each single input. I suppose reading the last number is fine, so lets leave that aside.

Next, get acustomed to the containers in the standard library. They are not many in numbers but what you can do with them is uncountable. To have distinct elements in a container you want either std::unsorted_set or std::set. Then basically all you need is to use insert:

#include <set>
#include <iostream>
int main()
{
    std::set<int> numbers;
    auto x = numbers.insert(1);
    std::cout << "1 was not present before? : " << x.second << "\n";
    x = numbers.insert(1);
    std::cout << "1 was not present before? : " << x.second << "\n";
}

prints:

1 was not present before? : 1
1 was not present before? : 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.