1

This is my code at the moment. It is a lottery game and I get user input for 7 numbers and do not allow duplicates (same goes with the random generated). I need to display the user's numbers and the winning random numbers at the end of the main next to LOTTO RESULTS and WINNING NUMBERS.

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

using namespace std;

void getLottoPicks(int userNums[], int size);
void genWinNums(int winNums[], int size);

int main()
{
    const int size = 7;
    int UserTicket[size];
    int WinningNums[size];
    char selection;
    string name;

    do
    {
        cout << "LITTLETON CITY LOTTO MODEL: " << endl;
        cout << "---------------------------" << endl;
        cout << "1) Play Lotto" << endl;
        cout << "q) Quit Program" << endl;
        cout << "Please make a selection : " << endl;
        cin >> selection;

        if (selection == '1')
        {
            cout << "Please enter your name: " << endl;
            cin.ignore();
            getline(cin, name);

            getLottoPicks(UserTicket, size);
            genWinNums(WinningNums, size);

            cout << name << "'s LOTTO RESULTS" << endl;
            cout << "----------------------" << endl;
            cout << "WINNING TICKET NUMBERS : " << endl;
            cout << name << "'s TICKET       : " << endl;

        }
        else if (selection == 'q')
        {
            cout << "You have chosen to quit the program. Thank you for using!" << endl;
        }
        else
        {
            cout << "Invalid selection. Please try again." << endl;
        }

    } while (selection != 'q');

    return 0;
}
void getLottoPicks(int userNums[], int size)
{
    for (int times = 0; times < size; times++)
    {
        int input;
        cout << "selection #" << times + 1 << ": " << endl;
        cin >> input;

        bool isNotDuplicate = true;
        for (int i = 0; i < times; i++)
        {
            if (userNums[i] == input)
            {
                 isNotDuplicate = false;
            }
        }
        if (isNotDuplicate == true)
        {
             userNums[times] = input;
        }   
        else
        {
             cout << "You already picked this number. Please enter a different number: " << 
             endl;
             times--;
        } 
     }  

}
void genWinNums(int winNums[], int size)
{
    srand((unsigned int)time(NULL));

    for (int times = 0; times < size; times++)
    {
        int i;
        bool isNotDuplicate = true;
        while (isNotDuplicate)
        {
            isNotDuplicate = false;
            i = 1 + rand() % 40;
            for (int j = 0; j < times; j++)
            {
                if (i == winNums[j])
                {
                    isNotDuplicate = true;
                }
            }
        }
        winNums[times] = i;
    }
}
2
  • Just like you would with any other array, use a loop and just print each element on its own Commented Aug 13, 2021 at 6:39
  • Instead of using an array pointer/size pair in your function prototypes, you can use std::array in C++11 onwards, something like typedef std::array<int, 7> LottoNumbers; instead. Commented Aug 13, 2021 at 6:56

2 Answers 2

1

It seems you might be new to programming so here you go, your working program:

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

using namespace std;

void getLottoPicks(int userNums[], int size);
void genWinNums(int winNums[], int size);

int main()
{
    const int size = 7;
    int UserTicket[size];
    int WinningNums[size];
    char selection;
    string name;

    do
    {
        cout << "LITTLETON CITY LOTTO MODEL: " << endl;
        cout << "---------------------------" << endl;
        cout << "1) Play Lotto" << endl;
        cout << "q) Quit Program" << endl;
        cout << "Please make a selection : " << endl;
        cin >> selection;

        if (selection == '1')
        {
            cout << "Please enter your name: " << endl;
            cin.ignore();
            getline(cin, name);

            getLottoPicks(UserTicket, size);
            genWinNums(WinningNums, size);

            cout << name << "'s LOTTO RESULTS" << endl;
            cout << "----------------------" << endl;
            cout << "WINNING TICKET NUMBERS : " << endl;
            for(int i = 0; i < size; i++){
                cout << WinningNums[i] << " ";
            }
            cout << endl;
            cout << name << "'s TICKET       : " << endl;
            for(int i = 0; i < size; i++){
                cout << UserTicket[i] << " ";
            }
            cout << endl;

        }
        else if (selection == 'q')
        {
            cout << "You have chosen to quit the program. Thank you for using!" << endl;
        }
        else
        {
            cout << "Invalid selection. Please try again." << endl;
        }

    } while (selection != 'q');

    return 0;
}
void getLottoPicks(int userNums[], int size)
{
    for (int times = 0; times < size; times++)
    {
        int input;
        cout << "selection #" << times + 1 << ": " << endl;
        cin >> input;

        bool isNotDuplicate = true;
        for (int i = 0; i < times; i++)
        {
            if (userNums[i] == input)
            {
                 isNotDuplicate = false;
            }
        }
        if (isNotDuplicate == true)
        {
             userNums[times] = input;
        }
        else
        {
             cout << "You already picked this number. Please enter a different number: " <<
             endl;
             times--;
        }
     }

}
void genWinNums(int winNums[], int size)
{
    srand((unsigned int)time(NULL));

    for (int times = 0; times < size; times++)
    {
        int i;
        bool isNotDuplicate = true;
        while (isNotDuplicate)
        {
            isNotDuplicate = false;
            i = 1 + rand() % 40;
            for (int j = 0; j < times; j++)
            {
                if (i == winNums[j])
                {
                    isNotDuplicate = true;
                }
            }
        }
        winNums[times] = i;
    }
}

As you can see, it is pretty easy to loop through an array. Maybe have a look at this for more info on arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey if you see this I just want you to know that you put a smile on my face by helping me. I was getting really annoyed and for some reason, that solution did not even cross my mind. thank you soo much dude!
done. This was my first time using this to post my own question lol.
1

Most of the examples show "classic" C++, instead of the more modern variants. So here's my take on your code :

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

// compile time constant, in many C++ examples this is done with a #define/MACRO
constexpr int number_of_lotto_numbers = 7; 
constexpr char play_lotto_char = '1';       
constexpr char quit_lotto_char = 'q';

// use std::array instead of int values[]; since this has automatic bound checking!
// no reading/writing beyond array limits is allowed
// I use, using here to make code a bit more readable further down the line
// it lets code show intent instead of implementation
using lotto_numbers_t = std::array<int, number_of_lotto_numbers>;

// do not return arrays by passing arguments, just return an array
// don't worry about this making extra (class) copies c++ knows how to optimize this
lotto_numbers_t get_lotto_picks()
{
    lotto_numbers_t lotto_numbers;
    auto lotto_numbers_entered{ 0 };

    while ( lotto_numbers_entered < number_of_lotto_numbers )
    {
        int input{ 0 };
        std::cout << "selection #" << lotto_numbers_entered + 1 << ": " << std::endl;
        std::cin >> input;

        // use std::find for finding items in collections, if it finds the end of a collection then 
        // the value is not found.
        if (std::find(lotto_numbers.begin(), lotto_numbers.end(), input) == lotto_numbers.end())
        {
            // lotto number not found so add it
            lotto_numbers[lotto_numbers_entered] = input;
            lotto_numbers_entered++;
        }
        else
        {
            // lotto number already in array so do not add it but give a message
            std::cout << "You already entered this number, try another number" << std::endl;
        }
    }

    return lotto_numbers;
}

lotto_numbers_t generate_winning_numbers()
{
    lotto_numbers_t lotto_numbers;
    auto lotto_numbers_generated{ 0 };

    std::srand((unsigned int)time(NULL));

    do
    {
        int new_number = (std::rand() % 40) + 1;
        if (std::find(lotto_numbers.begin(), lotto_numbers.end(), new_number) == lotto_numbers.end())
        {
            // number not yet found
            lotto_numbers[lotto_numbers_generated] = new_number;
            lotto_numbers_generated++;
        }

    } while (lotto_numbers_generated < number_of_lotto_numbers);

    return lotto_numbers;
}

void play_lotto()
{
    char selection{ 0 };    // always initialize variables!
    std::string name;

    do
    {
        std::cout << "LITTLETON CITY LOTTO MODEL: " << std::endl;
        std::cout << "---------------------------" << std::endl;
        std::cout << "1) Play Lotto" << std::endl;
        std::cout << "q) Quit Program" << std::endl;
        std::cout << "Please make a selection : " << std::endl;
    
        std::cin >> selection;

        if (selection == play_lotto_char)
        {
            std::cout << "Please enter your name: " << std::endl;
            std::cin.ignore();
            std::getline(std::cin, name);

            auto picked_numbers = get_lotto_picks();
            auto winning_numbers = generate_winning_numbers();


            std::cout << name << "'s LOTTO RESULTS" << std::endl;
            std::cout << "----------------------" << std::endl;
            std::cout << "WINNING TICKET NUMBERS : " << std::endl;
        
            for (const auto number : winning_numbers)
            {
                std::cout << number << " ";
            }
            std::cout << std::endl;

            std::cout << name << "'s TICKET       : " << std::endl;
            for (const auto number : picked_numbers)
            {
                std::cout << number << " ";
            }
            std::cout << std::endl;

            if (picked_numbers == winning_numbers)
            {
                std::cout << "you have won!" << std::endl;
            }
        }
        else if (selection == quit_lotto_char)
        {
            std::cout << "You have chosen to quit the program. Thank you for using!" << std::endl;
        }
        else
        {
            std::cout << "Invalid selection. Please try again." << std::endl;
        }

    } while (selection != quit_lotto_char);
}

Don't hesitate to ask questions on this code if you have any :)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.