Lottery Game

We have been studying arrays and I am very confused. A user enters one number, it is checked against the random number array, and then if the user input matches a number in the array, they win. I am having trouble with the check() function, and also the printOut() function as it should also print out the user input.
The assignment is 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.

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
// The Lottery
// Julia Mancuso
// 02-23-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 << "Are you going to win the lottery this week? Find out now!" << endl;
    int wins[arraySize];
    int userInput;
	
	entry(userInput);
	assign(wins, arraySize);
	draw(arraySize, wins);
	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 userInput, int count, int wins[]) {
	for (int i = 0; i < count; i++) {
		if (wins[i] == count)              
			return true;
	}
	return false;
}
void entry(int &userInput)  {
	int i;
	cout << "Enter your lottery number guess: ";
	cin >> userInput;
	cout << "Your number was " << userInput << endl;
}
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
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
73
74
75
76
77
78
79
80
81
82
83
84
// The Lottery
// Julia Mancuso
// 02-23-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[]);

// return true if number exists in the array
bool check( int number, const int array[], int array_sz );

void printOut( const int[], int );
void entry(int &);

int main()  {

    srand(time(nullptr));
    const int arraySize = 10;
    int user[arraySize];
    cout << "Are you going to win the lottery this week? Find out now!" << endl;
    int wins[arraySize];
    int userInput;

    entry(userInput);
    assign(wins, arraySize);
    draw(arraySize, wins);

    const bool win = check( userInput, wins, arraySize ) ;
    if(win) cout << "*** congratulations! you win. ***\n" ;

    printOut(wins, arraySize);
}

void assign(int wins[], int Size)   {

    for (int i = 0; i < Size; ++i)
    wins[i] = 0;
}

// fill array wins[] with unique random numbers in [1,1000]
void draw(int arraySize, int wins[])    {

    int count = 0;
    while (count < arraySize)   {

        int number = rand() % 1000 + 1;
        if( !check( number, wins, arraySize ) )  {

            wins[count] =  number;
            count++;
        }
    }
}

// return true if number exists in the array
bool check( int number, const int array[], int array_sz ) {

    for( int i = 0; i < array_sz ; ++i ) { // for each value in the array

        if( array[i] == number ) return true;
    }
    return false;
}

void entry(int &userInput)  {

    int i;
    cout << "Enter your lottery number guess: ";
    cin >> userInput;
    cout << "Your number was " << userInput << endl;
}

void printOut( const int wins[], int Size ) {

    cout << "Winning numbers in lottery are" << endl;
    for (int i = 0; i < Size; ++i)  {

        cout << wins[i] << " ";
    }
}
I updated it to this now, however I am still getting a compilation failed error of "main.cpp:(.text.startup+0x90): undefined reference to `printOut(int const*, 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
63
64
65
// The Lottery
// Julia Mancuso
// 02-25-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 , const int[] , int);
void printOut(const int[], int);
void entry(int &);
 
int main()  {

    srand(time(nullptr));
    const int arraySize = 10;
	int user[arraySize];
	cout << "Are you going to win the lottery this week? Find out now!" << endl;
    int wins[arraySize];
    int userInput;
	
	entry(userInput);
	assign(wins, arraySize);
	draw(arraySize, wins);
	const bool win = check(userInput, wins, arraySize);
    if (win)
        cout << "Congratulations! You've won!" << endl;     
	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(number, wins, arraySize))   {
			wins[count] =  number;
			count++;
		}
	}
}
bool check(int number, const int wins[], int arraySize) {
	for (int i = 0; i < arraySize; i++) {
		if (wins[i] == number)              
			return true;
	}
	return false;
}
void entry(int &userInput)  {
	int i;
	cout << "Enter your lottery number guess: ";
	cin >> userInput;
	cout << "Your number was " << userInput << endl;
}
void printOut(int wins[], int Size) {
	cout << "Winning numbers in lottery are" << endl;
	for (int i = 0; i < Size; ++i)  {
		cout << wins[i] << " ";
	}
}
Line 13: void printOut(const int[], int);
Line 60: void printOut(int wins[], int Size) {
They have different signatures, and therefore don't match. Add const to both, or remove const from both.
Thank you so much for your help!
Topic archived. No new replies allowed.