Help with Lottery program

I am having trouble with this program. It works correctly if the user loses but not if the user wins. If the user wins it prints out nothing.




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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

void initialize (int [], int, int);
int check (int [], int, int);
void draw (int[], int);
int entry ();
void printOut (int [], int, int);

int main(){
	const int size = 10;
	int wins [size];
	int initVal=-1;
	int guess;
	srand(time(NULL));

	initialize (wins, size, initVal);
	draw (wins, size);
	guess = entry ();
	int f = check (wins, size, guess);
	if (f==-1){
		cout << "You didn't win the lottery! Better luck next time..." << endl;
		printOut (wins, size, guess);
	}
	if (f==guess){
		cout << "You won the lottery!! Congratulations!!" << endl;
		printOut (wins, size, guess);
	}
}

void initialize (int a[], int n, int val){
	for (int i=0; i < n; i++)
		a[i] = val;
}

int check (int a[], int n, int value){
	for (int i=0; i < n; i++)
	{
		if (a[i] == value)
			return i;
	}

	return -1;
}

void draw (int a[], int n){
	int numSoFar = 0;
	while (numSoFar < n){
		int num = rand()%100;
		int e = check (a, n, num);
		if (e == -1){
			a[numSoFar] = num;
			numSoFar++;
		}
	}
}

int entry (){
	int userInput;
	cout << "Enter your guess <0-99>: ";
	cin >> userInput;
	return userInput;
}

void printOut (int a[], int n, int user){
	cout << "The winning numbers were: ";
	for (int i=0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
}
line 42: change return i; to return value;

because you want check() to return guess value in line 27
Two problems that I see:

1) You call check at line 22. check returns the index of the matching entry (line 42).
At line 27, you compare the returned value to the guess. They won't match unless the value and the index it's stored at are the same.

2) At line 52 you call check passing n as the number of entries to check. n is the size of the array, not the number of entries currently in it. Should be:
 
int e = check (a, numSoFar, num);


You should use the integer returned by the function in the subscript operator with the array 'wins'. This will access the correct element of the array and compare it to the user's guess. Although, that and the second if statement within the main function's compound statement is unnecessary. The check function will return the integer '-1' if the user's guess did not match any of the winning lottery numbers, so you could simply move the contents of the second if statement's compound statement to the end of main, remove the second if statement, and then add a return statement to the end of the first if statement to achieve the same result.

1
2
3
4
if (wins[f]==guess){
		cout << "You won the lottery!! Congratulations!!" << endl;
		printOut (wins, size, guess);
}
Last edited on
Thank you everyone! Makes sense now, just a dumb mistake I didn't notice.
Topic archived. No new replies allowed.