Lottery program

Hello I am supposed to write a code that asks the user to input a number between 0-99 and hopefully win the lottery. If the number they input matches one of the 10 random numbers they win.

I am having problems assigning the random numbers to the array.
https://i.gyazo.com/fb9fbf75f6c76a819cfa64aea08ef827.png
Not very sure what step to take next if anyone can help, thank you!I think it is an issue with my printOut or draw function

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
 #include <iostream>
#include <ctime>
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(){
	//declare array and other variables
	srand(time(NULL));
	const int arraySize = 5;
	int a[arraySize];
	cout << "Lottery game! guess a number" << endl;
	int wins[arraySize];
	int userInput;
	entry(userInput);


	assign(a, arraySize); // fill array with -1
	draw(arraySize, wins);       // select 10 non-repeating random numbers
	check(userInput, arraySize, a);     
	printOut(a, arraySize);   // outputs seleced lottery numbers



}
void assign(int a[], int size){
	for (int i = 0; i < size; i++)
	 a[i]=-1;
}
void draw(int arraySize, int wins[])
{
	int count = 0;
	while (count < arraySize)
	{
		int num = rand() % 100;
		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 <0-99>: ";
	cin >> userInput;
}
Last edited on
random number generation facilities through header file <random> is generally preferred - search this forum last month or so for several discussions:
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
#include <iostream>
#include <algorithm>
#include <random>
#include <chrono>

//using namespace std; http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

auto constexpr SIZE = 10;

int main()
{
    int arr[SIZE];

    auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
    std::default_random_engine dre(seed);//random number generation engine
    std::uniform_int_distribution<int> uid(0,99);//distribution
    http://stackoverflow.com/questions/7114043/random-number-generation-in-c11-how-to-generate-how-do-they-work

    std::generate(arr, arr + sizeof(arr) / sizeof(int), [&] () { return uid(dre); });
    //http://en.cppreference.com/w/cpp/algorithm/generate

    for (const auto& elem : arr)
    {
        std::cout << elem << " ";
    }
}

I'm guessing you just want something pretty basic. Use a 'for' loop to enter data into an array:

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
#include <cstdlib>
#include <iostream>      
#include <time.h>

using namespace std;

int main(int argc, char** argv) {

    srand(time(NULL));

    int lotto[10];

    for (int i = 0; i < 10; i++) {
        
        int l1 = rand() % 100;
        lotto[i] = l1;
    }

    for (int i = 0; i < 10; i++) {
        cout << lotto[i] << " ";
    }

    cout << endl;

    return 0;
}
Topic archived. No new replies allowed.