Lottery outputting only 1 number

Writing a lottery program that has an array size of 20 that can select numbers from 1-100. The main function must include:
main(){
declare array and other variables

assign(...) // fill array with 0
draw(...) // select 20 non-repeating random numbers
entry(...) // get user input
use check () to compare user input against lottery numbers
and output whether user won
printOut(...) // outputs selected 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
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout; using std::cin; using std::endl;

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(NULL));
	const int arraySize = 20;
	int a[arraySize];
	cout << "Lottery game! Guess a number: " << endl;
	int wins[arraySize];
	int userInput;
	entry(userInput);
	assign(a, arraySize); 
	draw(arraySize, wins);     
	check(userInput, arraySize, a);
	printOut(a, arraySize);   
}
void assign(int a[], int size) {
	for (int i = 0; i < size; ++i)
		a[i];
}
void draw(int arraySize, int wins[])
{
	int count = 0;
	while (count < arraySize)
	{
		int num = rand() % 100+1;
		if (!check(num, arraySize, wins))
		{
			wins[count] = num;
			++count;
		}
	}
}
bool check(int num, int arraySize, int wins[]) {
	for (int i = 0; i < arraySize; ++i)
	{
		if (wins[i] == num)
			return true;
	}
	return false;
}
void printOut(int wins[], int size)
{
	cout << "winning numbers in lottery are" << endl;
	for (int i = 0; i < size; ++i)
	{
		cout << wins[i];
	}

}
void entry(int &userInput) {
	cout << "Enter your guess <1-100>: " << endl;
	cin >> userInput;
}
Last edited on
Hello urawrath,

Suggestion: on line 14 put "arraySize" in all caps. This lets you and others know it is defined as a constant.

Line 15 can be written as: int a[arraySize]{};. The {}s will initialize every element of the array to zero. The same for line 17.

The function "assign" assigns nothing to the array and if you use the above mentioned initialization this function will not be needed.

In the "check" and "printOut" functions just because you call the array "wins" in the function definition does not mean that it is the array that you will be using. Back in main you sent the array "a" to the function. At the function you can call it anything you want, but it is still the "a" array that was sent and used. Both these functions are using the wrong arrays.

In the end there seams to be no real use of array "a".

Hope that helps,

Andy
Hello urawrath,

I straightened out your program and then looked at the instructions.

You do need the "assign" function, but it needs to actually assign something to each element of the array.

Then I noticed that the function calls in your code are out of order based on the instructions.

The "check" function returns a "bool" which you do not capture and you do not use it to let the user know if he/she has won or lost.

Otherwise the program is well done.

Hope that helps,

Andy

P.S. Put a comment on your "srand" and use "srand(42)" for testing. This way each time the program starts the numbers will be the same. It makes testing easier.
Last edited on
Sorry for the late reply, thank you for helping sort this and I will start to make the changes and see where I can go and thank you for the tip about srand!
Fixed everything! thank you! also added cout << wins[i] << " "; to space everything out
Hello urawrath,

You are welcome. Good job.

Andy
Topic archived. No new replies allowed.