Program to simulate lottery help!

Hello! I am extremely new to programming and have a question about some code I've been working on. For my homework, I need to simulate a lottery draw of five number (using random numbers 0-9), ask the user for their numbers, compare and display both sets of numbers and count the number of matches. The issue I'm having is when it comes to counting the matches. I just can't get the counter to work properly. I've tried for loops and writing the code literally (if (lottery[0]==user[0])…)and nothing has worked properly. Also, I am aware that using namespace is frowned upon but my instructor insists upon it for this beginner course. Here is the full code:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  #include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int number = 5;
	int lottery[number];
const int value = 5;
	int user[value];
	int numMatches = 0;

void generateNumbers()
{
	//seed the random number.
	srand(time (NULL));

	for (int i = 0; i < number; i++)
	{
		lottery[0] = rand() % 10;
		lottery[1] = rand() % 10;
		lottery[2] = rand() % 10;
		lottery[3] = rand() % 10;
		lottery[4] = rand() % 10;

	}

}


void getUser()
{
		cout << "Enter five numbers(0-9):\n";
		cin >> user[0];
		cin >> user[1];
		cin >> user[2];
		cin >> user[3];
		cin >> user[4];
	
}

void displayValues()
{
	//display generated numbers.
	cout << "Winning numbers:\n";
	for (int i = 0; i < number; i++)
	{
		cout << " " << lottery[i];
		cout << endl;
	}
	//display user entered numbers
	cout << "Your numbers:\n";
	for (int i = 0; i < value; i++)
	{
		cout << " " << user[i];
		cout << endl;
	}
}

int main()
{
	int lottery[number];
	int user[value];
	int i = 0;

	//generate the random lottery numbers.
	generateNumbers();

	//get the user's numbers.
	getUser();

	//get the number of matching numbers.
	for (i = 0; i < 5; i++)
	{
		if (user[i] == lottery[i])
		{
			numMatches++;
		}
	}
	//display the lottery and user numbers.
	displayValues();

	//display the number of matching numbers.
	if (numMatches != 5)
	{
		cout << "You matched " << numMatches << " numbers.\n";
	}

	//determine whether the user won the grand prize.
	if (numMatches == 5)
		cout << "Congratulations! You're the grand prize winner!\n";
	return 0;
}
Hello Kscronce,

After looking at your code for a while I figured what your problem was:
You're creating two instances of the arrays lottery and user. You have for both, one global variable and one variable in main.

To fix your code and make it work I simply did that:

1
2
3
4
5
6
int main()
{
	//int lottery[number];
	//int user[value];
	int i = 0;
//[...] 


EDIT: Some people are probably going to mention to you that you should avoid using global variables. So what you could do in this case is delete your globals and keep the two arrays you defined in your main function. You would then need to pass them as argument in your functions generateNumbers, getUser and displayValues.

Also I don't know why you included "stdafx.h" but I also removed it to compile your program and it works just fine.

Hope this helps,

Regards,

Hugo.
Last edited on
There's nothing wrong with "using namespace std" in a short program, especially for beginners.

{Erased post since OP is an idiot. Left above since mbozzi commented on it.}
Last edited on
You fixed every part of my code that was already working fine( except for the for loop used to generate the numbers, thanks for pointing that one out). The part I'm trying to sort out is the match counter. When I enter numbers the program says there are no matches even if there are.
Last edited on
Re-read my comment :)
There's nothing wrong with "using namespace std" in a short program, especially for beginners.

I disagree.

A standard library header must introduce certain names, but it may also introduce names from anywhere else in the standard library. If a user borrows an unqualified name from the standard library, it's quite possible that the result works on the student's machine but not on the instructor's.

At best the program won't compile. At worst, the program silently uses the wrong type or calls the wrong function. IMO, it's best to just use unambiguous names, and leave using x to mean "I want ADL here".

Here's one example of this happening on this forum.
http://www.cplusplus.com/forum/beginner/205160/#msg972411
Last edited on
@mbozzi, That pretty much makes "using namespace" a misfeature. Thanks for the example.
figured it out on my own.
Thanks anyway guys!
Last edited on
What a piece of human garbage.
Well that’s excessively rude. So I’m a piece of human garbage for what reason exactly? This stuff doesn’t come easy to me so it does make me a little edgy. Especially so when I’m getting responses that are, for the most part not working out or are not really helpful. My last comment only meant that I figured it out on my own but I still appreciate people taking the time to look at it.
Topic archived. No new replies allowed.