Very Basic Lottery

I'm trying to make a lottery in C++

The following functions i tried to create:

initialize() that takes array wins[] as parameter and assigns -1 to each element of the array.

check() that takesa number and the array wins[] as parameters and returns true if the number matches one of the elements in the array, or false if none of the elements do. You may assume that the number check() is looking for a function check() to make sure that the new number is not already in the array.

entry() that asks the user to enter a single number from 0 to 99 and returns this value.

printOut() that ouputs the selected numbers and user input.

and finally: the pseudo code for the main function:
main(){
declare array and other variables

initialize(..) // Fill array with -1
draw(..) //Select 10 non-repeating random numbers.
entry(...) //get user input.
use check() to compare the user input against lottery numbers and output whether user won
printOut(..) //Outputs selected numbers
}

But i just completely suck... Can someone help?
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>

const int ARRAY_SIZE = 10, NUM_LOTTERY_NUMBERS = 100, NOT_FOUND = -1;

void initialize (int array[], int size = ARRAY_SIZE) {
	for (int i = 0; i < size; i++)
		array[i] = NOT_FOUND;
}

void draw (int array[], int size = ARRAY_SIZE) {
	std::vector<int> lotteryNumbers (NUM_LOTTERY_NUMBERS);
	std::iota (lotteryNumbers.begin(), lotteryNumbers.end(), 0);
	std::random_shuffle (lotteryNumbers.begin(), lotteryNumbers.end());
	for (int i = 0; i < size; i++)
		array[i] = lotteryNumbers[i];	
}

bool check (int array[], int draw, int size = ARRAY_SIZE) {
	return std::any_of (array, array + size, [=](int x)->bool {return x == draw;});
}

void printOut (int array[], int size = ARRAY_SIZE) {
	for (int i = 0; i < size; i++)
		std::cout << array[i] << std::endl;	
}

int entry() {
	int num;
	std::cout << "Please enter a number from 0 to " << NUM_LOTTERY_NUMBERS - 1 << ": " << std::endl;
	std::cin >> num;
	return num;	
}

int main() {
	std::srand (std::time (nullptr));
	int lottery[ARRAY_SIZE];
	initialize (lottery);
	while (true)
	{
		draw (lottery);
		const int number = entry();
		const bool youWon = check (lottery, number);
		if (youWon)
			std::cout << "You won!" << std::endl;
		else
			std::cout << "You did not win." << std::endl;
		std::cout << std::endl << "Here are the lottery numbers:" << std::endl;
		printOut (lottery);
	}
}


Compiled on GCC 4.8.1. Sample output:
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
Please enter a number from 0 to 99:
1
You won!

Here are the lottery numbers:
12
1
9
98
96
27
58
82
86
90
Please enter a number from 0 to 99:
5
You did not win.

Here are the lottery numbers:
58
10
53
77
49
51
29
28
30
74
Please enter a number from 0 to 99:
Last edited on
Topic archived. No new replies allowed.