Lottery Generator

Pages: 12
I want to make a lottery generator and compare both of them together to see if the user is a winner or not. I think I'm on the right track but I'm getting an error message in from my compiler "error C2440: '=' : cannot convert from 'void' to 'int'". Can anyone please help? thanks.

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


void getUserNumber(int [], int);
void randomNumber();

int main()
{

	const int SIZE = 5;
	int lottery[SIZE];
	int user[SIZE];
	int userNum;
	int lotteryNum;

	//Get user numbers
	userNum = getUserNumber(user, SIZE);
	cout << userNum << endl;

	lotteryNum = randomNumber();
	cout << lotteryNum << endl;


	return 0;
	}

void randomNumber()
{
	int lottery[9];
	unsigned seed = time(0);
	srand(seed);

	for (int number = 0; number < 5; number++)
	{
		lottery[number] = 1 + rand() % 9;
		cout << lottery[number] << " ";
	}
}

void getUserNumber(int user[], int size)
{
	int userNum;

	//Get user numbers
	for (int userNum = 0; userNum < 5; userNum++)
		{
			cout << "Enter your numbers " << (userNum + 1) << ": ";
			cin >> user[userNum];

		}
}
closed account (Lv0f92yv)
It looks like the function getUserNumber is declared to return a 'void' type - that is, no value. It must be declared to return an 'int' in this case, since that is the type of data you want to store to userNum.

Edit: If you want to return more than one number, consider returning an array instead of just one int. This should be done with a return statement.

1 Number Example:

1
2
3
4
5
6
7
...
int myFunc( )
{
   int myNum = 5;
   return myNum;
}
...


For arrays (more than one number):

1
2
3
4
5
6
7
...
int* myFunc()
{
   int a[] = {4, 5, 3, 6}; 
   return a;
}
...

(The above is only meant as an example, keep in mind it is only returning the address of a temporary variable - for your situation it looks like you'll return the address of the user variable, since that is what's being modified.)

Hope that helps.
Last edited on
Thank you for your quick reply. I changed all the voids to int and I get more errors.
warning C4101: 'lottery' : unreferenced local variable
warning C4244: 'initializing' : conversion from 'time_t' to 'unsigned int', possible loss of data
warning C4101: 'userNum' : unreferenced local variable
error C4716: 'randomNumber' : must return a value
error C4716: 'getUserNumber' : must return a value

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


int getUserNumber(int [], int);
int randomNumber();

int main()
{

	const int SIZE = 5;
	int lottery[SIZE];
	int user[SIZE];
	int userNum;
	int lotteryNum;

	//Get user numbers
	userNum = getUserNumber(user, SIZE);
	cout << userNum << endl;

	lotteryNum = randomNumber();
	cout << lotteryNum << endl;


	return 0;
	}

int randomNumber()
{
	int lottery[9];
	unsigned seed = time(0);
	srand(seed);

	for (int number = 0; number < 5; number++)
	{
		lottery[number] = 1 + rand() % 9;
		cout << lottery[number] << " ";
	}
}

int getUserNumber(int user[], int size)
{
	int userNum;

	//Get user numbers
	for (int userNum = 0; userNum < 5; userNum++)
		{
			cout << "Enter your numbers " << (userNum + 1) << ": ";
			cin >> user[userNum];

		}
}
warning C4101: 'userNum' : unreferenced local variable


You have int userNum declared twice in the getUserNumber function. In the for loop remove "int userNum = 0" and replace it with just "userNum = 0"

error C4716: 'randomNumber' : must return a value
error C4716: 'getUserNumber' : must return a value


The problem is your int functions are not returning a value. You need to return an integer for these to work.

For instance, here's an example which will return a random number.
1
2
3
4
5
6
7
8
9
10
11
12
13

int lotteryNumber;

lotteryNumber = randomNumber();

int randomNumber()
{
     int number;
 
     number = 1 + rand() % 9;

     return number;
}


closed account (Lv0f92yv)
I also noticed that you don't need to return anything with getUserNumbers(), since you are passing it the address of the thing you want to modify (and are doing so with cin). This changes the contents at that address, so you should then (after the function call), be able to get those values by iterating over user[...].

That function can be done without returning a value, in this case.
Ok, I put a return value in each function. I need to return an array of 5 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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int getUserNumber(int [], int);
int randomNumber();

int main()
{

	const int SIZE = 5;
	int lottery[SIZE];
	int user[SIZE];
	int userNum;
	int lotteryNum;

	//Get user numbers
	userNum = getUserNumber(user, SIZE);
	cout << userNum << endl;

	lotteryNum = randomNumber();
	cout << lotteryNum << endl;


	return 0;
	}

int randomNumber()
{
	int lottery[9];
	unsigned seed = time(0);
	srand(seed);

	for (int number = 0; number < 5; number++)
	{
		lottery[number] = 1 + rand() % 9;
		cout << lottery[number] << " ";
	}
	return lottery[9];
}

int getUserNumber(int user[], int size)
{
	int userNum;

	//Get user numbers
	for (userNum = 0; userNum < 5; userNum++)
		{
			cout << "Enter your numbers " << (userNum + 1) << ": ";
			cin >> user[userNum];

		}
	return user;
}
closed account (Lv0f92yv)
I need to return an array of 5 numbers.


My observation was late, for that I apologize. As I stated in my late followup, you don't need to return a value to anybody for getUserNumbers( ..., ... ) since you are passing it the array - and this the address of the data you want to edit (the user numbers). Use a for loop to iterate over user[...] after the function call and you should be able to access he numbers inputted.

Edit: Remember to not try and store anything with something = getUserNumbers(..., ...); if you're return type is void - it should just be the function call by itself.

I think this should work for what you are trying to do.
Last edited on
Building on Desh's post, getUserNumber can be void instead of type int. This is because arrays are passed to functions by reference, not by value. Consequently you can remove "return user", since a function of type void does not return anything. This will allow you to remove userNum from the main function as well. To print out the array just add a for loop, as Desh stated.

Ok I understand more about this now. What would my for loop look like? Thanks.
Assuming it's just to print out the contents of the array, something like this:

1
2
3
4
5
6
getUserNumber(user, SIZE);

for ( i = 0; i < 5; i++ )
{
     cout << user[i] << endl;
}
Thank you very much. One more error and I think we are cooking with gas.

error C2660: 'randomNumber' : function does not take 0 arguments

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


void getUserNumber(int [], int);
int randomNumber(int);

int main()
{

	const int SIZE = 5;
	int lottery[SIZE];
	int user[SIZE];
	int userNum;
	int lotteryNum;

	//Get user numbers
	getUserNumber(user, SIZE);

	for (int i = 0; i < 5; i++)
	{
		cout << user[i] << endl;
	}

	lotteryNum = randomNumber();
	cout << lotteryNum << endl;

	return 0;
	}

int randomNumber(int size)
{
	int lottery[9];
	unsigned seed = time(0);
	srand(seed);

	for (int number = 0; number < size; number++)
	{
		lottery[number] = 1 + rand() % 9;
		cout << lottery[number] << " ";
	}
	return lottery[9]; 
}

void getUserNumber(int user[], int size)
{
	int userNum;
	
	//Get user numbers
	for (userNum = 0; userNum < size; userNum++)
		{
			cout << "Enter your numbers " << (userNum + 1) << ": ";
			cin >> user[userNum];
		}

}
The cause of the error is line 27. You declared randomNumber to take an argument of type int, but aren't passing it anything.

Change
 
lotteryNum = randomNumber();


to

 
lotteryNum = randomNumber(SIZE);


You can also delete int userNum from main, since it is not longer being used.
I made the changes and it works!! Well, sort of. My output is this:

User Numbers:
1 2 3 4 5

Lottery Numbers:
6 3 7 7 2 -858993460

How do i get rid of the -858993460? I'm sorry its so basic and I suck at this.

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


void getUserNumber(int [], int);
int randomNumber(int);

int main()
{

	const int SIZE = 5;
	int lottery[SIZE];
	int user[SIZE];
	int lotteryNum;

	//Get user numbers
	getUserNumber(user, SIZE);
	cout << "User Numbers: \n";

	for (int i = 0; i < 5; i++)
	{
		cout << user[i] << " ";
	}
	cout << "\n\n";
	cout << "Lottery Numbers: \n";
	lotteryNum = randomNumber(SIZE);
	cout << lotteryNum << endl;

	return 0;
	}

int randomNumber(int size)
{
	int lottery[5];
	unsigned seed = time(0);
	srand(seed);

	for (int number = 0; number < size; number++)
	{
		lottery[number] = 1 + rand() % 9;
		cout << lottery[number] << " ";
	}
	
	return lottery[5]; 
}

void getUserNumber(int user[], int size)
{
	int userNum;
	
	//Get user numbers
	for (userNum = 0; userNum < size; userNum++)
		{
			cout << "Enter your numbers " << (userNum + 1) << ": ";
			cin >> user[userNum];
		}

}
I should have caught this early, so I apologize. In line 28 you are assigning an int array (the return value from randomNumber) to an int (lotteryNum). This is what is causing the -858993460. To fix this, change randomNumber from type int to type void and remove its return value. Also remove line 29 which prints out lotteryNum. That is not needed; what you are printing out in the randomNumber function will suffice. You can also remove int lotteryNum and int lottery[SIZE] altogether. It should now run properly.

1
2
lotteryNum = randomNumber(SIZE);
cout << lotteryNum << endl;


Needs to be changed to just the function call of randomNumber(SIZE);
I though it was going to be easy to add a check for winner. I get one error message:

error C2120: 'void' illegal with all types

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


void getUserNumber(int [], int);
void randomNumber(int);

int main()
{

	const int SIZE = 5;
	int user[SIZE];
	int lotteryNum;

	//Get user numbers
	getUserNumber(user, SIZE);
	cout << "User Numbers: \n";
	for (int i = 0; i < 5; i++)
		{
			cout << user[i] << " ";
		}
	cout << "\n\n";
	cout << "Lottery Numbers: \n";
	randomNumber(SIZE);

	bool arraysEqual = true;
	int count = 0;

	while (arraysEqual && count < SIZE)
	{
		if(getUserNumber(user, SIZE) != randomNumber(SIZE))
			arraysEqual = false;
		count++;
	}
	
	if (arraysEqual)
		cout << "You are the Grand Prize Winner!!!";
	else
		cout << "I'm sorry you are not a winner.  Thank you for playing";

	return 0;
}

void randomNumber(int size)
{
	int lottery[5];
	srand((unsigned)time(0));

	for (int number = 0; number < size; number++)
	{
		lottery[number] = 1 + rand() % 9;
		cout << lottery[number] << " ";
	}
	 
}

void getUserNumber(int user[], int size)
{
	int userNum;
	
	//Get user numbers
	for (userNum = 0; userNum < size; userNum++)
		{
			cout << "Enter your numbers " << (userNum + 1) << ": ";
			cin >> user[userNum];
		}

}
Your getUserNumber returns void. Have it return int instead, because you can't do what you're doing now at line 33.

-Albatross

EDIT: 2^9 posts, and counting.
Last edited on
if I change getUserNumber and randomNumber to int, I would need a return value
Alternatively, you could remove int lottery from randomNumber and place it in main. Then pass lottery to randomNumber. This would allow you to keep randomNumber and getUserNumber as void. Then you can replace your while loop with a for loop that compares each element of lottery with each element of user and checks to see if there is a match or not.
Yes. But the way you have it, it just doesn't work. You can't do comparisons between nothing in C++.

Also, if you're willing to read a little bit, Archaic's solution is another possibility.
http://cplusplus.com/doc/tutorial/functions2/

You will not be able to do the comparison as it is now, though, so one way or another you will need to make a fundamental change to your code. :)

-Albatross
Last edited on
I done everything except the for loop. I'm not great on for loops, so can you help me with it?

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


void getUserNumber(int [], int);
void randomNumber(int [], int);

int main()
{

	const int SIZE = 5;
	int user[SIZE];
	int lottery[SIZE];

	//Get user numbers
	getUserNumber(user, SIZE);
	cout << "User Numbers: \n";
	for (int i = 0; i < 5; i++)
		{
			cout << user[i] << " ";
		}
	cout << "\n\n";
	cout << "Lottery Numbers: \n";
	randomNumber(lottery, SIZE);

	bool arraysEqual = true;
	int count = 0;

	while (arraysEqual && count < SIZE)
	{
		if(getUserNumber(user, SIZE) != randomNumber(SIZE))
			arraysEqual = false;
		count++;
	}
	
	if (arraysEqual)
		cout << "You are the Grand Prize Winner!!!";
	else
		cout << "I'm sorry you are not a winner.  Thank you for playing";

	return 0;
}

void randomNumber(int lottery[], int size)
{
	srand((unsigned)time(0));

	for (int number = 0; number < size; number++)
	{
		lottery[number] = 1 + rand() % 9;
		cout << lottery[number] << " ";
	}
	 
}

void getUserNumber(int user[], int size)
{
	
	//Get user numbers
	for (int userNum = 0; userNum < size; userNum++)
		{
			cout << "Enter your numbers " << (userNum + 1) << ": ";
			cin >> user[userNum];
		}

}
Pages: 12