Lottery Ticket Check

Hello, the assignment instructions are as follows: In this assignment you are to code a program that selects 10 random non-repeating positive numbers (the lottery numbers), takes a single input from the user and checks the input with the ten lottery numbers. The lottery numbers range from 1 to 1000. The user wins if her input matches one of the lottery numbers.
I've written most of the code but I've gotten stuck at this point. We are supposed to use arrays. If anybody has any tips to help me out I would greatly appreciate it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// The Lottery
// Julia Mancuso
// 02-20-2018

#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout; using std::endl; using std::cin;

void assign(int[], int Size);
void draw(int, int[]);
bool check(int , int , int []);
void printOut(int[], int);
void entry(int[]);
 
int main()  {

    srand(time(nullptr));
    const int arraySize = 10;
	int user[arraySize];
	cout << "Did you win the lottery this week? Check it out now!" << endl;
    int wins[arraySize];
	
	assign(wins, arraySize);
	draw(arraySize, wins);
	entry(arrayInput);
	check(userInput, arraySize, wins);     
	printOut(wins, arraySize);
}
void assign(int wins[], int Size)   {
	for (int i = 0; i < Size; ++i)
	 wins[i] = 0;
}
void draw(int arraySize, int wins[])    {
	int count = 0;
	while (count < arraySize)   {
		int number = rand() % 1000 + 1;
		if (!check(count, arraySize, wins))   {
			wins[count] =  number;
			count++;
		}
	}
}
bool check(int count, int arraySize, int wins[]) {
	for (int i = 0; i < arraySize; ++i) {
		if (wins[i] == count)              
			return true;
	}
	return false;
}
void entry(int user[])  {
	int i;
	cout << "Enter your 10 lottery numbers: ";
	cin >> user[i];
}
void printOut(int wins[], int Size) {
	cout << "Winning numbers in lottery are" << endl;
	for (int i = 0; i < Size; ++i)  {
		cout << wins[i];
	}
}
Last edited on
What are you stuck on, specifically?

Also, please edit your OP and add [code] code tags [/code] tags around your code.

The problem with using rand() by itself like that is you can still have a repeating random number (it could draw 1 twice, for example). To prevent this, you could fill a lottery array in advance, and then shuffle the numbers to randomize their order, but still keep every number unique.

Then, the first 10 elements of the lottery ticket array can be the winning numbers.

Here's a simple shuffle implementation. The standard library has one that works with rand() too, but it's deprecated (iirc). I would suggest using C++11 <random> utilities, but since you're already using rand(), we'll just stick with that for now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Example program
#include <iostream>
#include <string>


void shuffle(int array[], int size)
{
    for (int i = 0; i < size; i++)
    {
        // get random index:
        int index = rand()%size;  
        
        // swap:
        int temp = array[i];
        array[i] = array[index];
        array[index] = temp;
    }
}

int main()
{
    srand(time(nullptr));
    
    const int Num_Tickets = 1000;
    int lottery_tickets[Num_Tickets];
    for (int i = 0; i < Num_Tickets; i++)
    {
        lottery_tickets[i] = i + 1;
    }
    
    shuffle(lottery_tickets, Num_Tickets);
    
    const int Num_Winning_Tickets = 10;
    
    std::cout << "The winning tickets are:";
    for (int i = 0; i < Num_Winning_Tickets; i++)
    {
       std::cout << " " << lottery_tickets[i];
    }
    std::cout << "\n";
    
}


_____________________________

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Example program
#include <iostream>
#include <string>


void shuffle(int array[], int size)
{
    for (int i = 0; i < size; i++)
    {
        // get random index:
        int index = rand()%size;  
        
        // swap:
        int temp = array[i];
        array[i] = array[index];
        array[index] = temp;
    }
}

bool check_if_winner(int user_input[], int lottery_tickets[], int num_winning_tickets)
{
    for (int i = 0; i < num_winning_tickets; i++)
    {
        int user_ticket = user_input[i];
        for (int j = 0; j < num_winning_tickets; j++)
        {
            if (user_ticket == lottery_tickets[j])
            {
                std::cout << "Winner! You matched " << lottery_tickets[j] << std::endl;
                return true;
            }
        }  
    }
    return false;
}

int main()
{
    srand(time(nullptr));
    
    const int Num_Tickets = 1000;
    int lottery_tickets[Num_Tickets];
    for (int i = 0; i < Num_Tickets; i++)
    {
        lottery_tickets[i] = i + 1;
    }
    
    shuffle(lottery_tickets, Num_Tickets);
    
    const int Num_Winning_Tickets = 10;
    
    std::cout << "The winning tickets are:";
    for (int i = 0; i < Num_Winning_Tickets; i++)
    {
       std::cout << " " << lottery_tickets[i];
    }
    std::cout << " <TODO: Don't show the user this in advance> \n";
    
    // Get user input:
    int user_input[Num_Winning_Tickets];
    std::cout << "Enter your 10 lottery numbers: ";
    for (int i = 0; i < Num_Winning_Tickets; i++)
    {
        std::cin >> user_input[i];
    }
    
    if (!check_if_winner(user_input, lottery_tickets, Num_Winning_Tickets))
    {
        std::cout << "Sorry, you didn't win. But feel free to waste more money next time!" << std::endl;
    }

}
Last edited on
I updated it slightly since I posted, this is what I have now. When I run the code though, the error comes up as "./ccPcZT4x.o: In function `main':
main.cpp:(.text.startup+0x64): undefined reference to `entry(int&)'
collect2: error: ld returned 1 exit status"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// The Lottery
// Julia Mancuso
// 02-20-2018

#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout; using std::endl; using std::cin;

void assign(int[], int Size);
void draw(int, int[]);
bool check(int , int , int []);
void printOut(int[], int);
void entry(int &);
 
int main()  {

    srand(time(nullptr));
    const int arraySize = 10;
	int user[arraySize];
	cout << "Did you win the lottery this week? Check it out now!" << endl;
    int wins[arraySize];
    int userInput;
	
	assign(wins, arraySize);
	draw(arraySize, wins);
	entry(userInput);
	check(userInput, arraySize, wins);     
	printOut(wins, arraySize);
}
void assign(int wins[], int Size)   {
	for (int i = 0; i < Size; ++i)
	 wins[i] = 0;
}
void draw(int arraySize, int wins[])    {
	int count = 0;
	while (count < arraySize)   {
		int number = rand() % 1000 + 1;
		if (!check(count, arraySize, wins))   {
			wins[count] =  number;
			count++;
		}
	}
}
bool check(int count, int arraySize, int wins[]) {
	for (int i = 0; i < arraySize; ++i) {
		if (wins[i] == count)              
			return true;
	}
	return false;
}
void entry(int user[])  {
	int i;
	cout << "Enter your 10 lottery numbers: ";
	cin >> user[i];
}
void printOut(int wins[], int Size) {
	cout << "Winning numbers in lottery are" << endl;
	for (int i = 0; i < Size; ++i)  {
		cout << wins[i];
	}
}
Last edited on
Please edit your OP and add code tags tags around your code.

Please edit your post and add [code] code tags [/code] tags around your code.

Your function prototype is: void entry(int &);
Your function definition is: void entry(int user[])
These are not equivalent. Looks like you want to change the former to void entry(int[]);
Topic archived. No new replies allowed.