cant find a problem in 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
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
#include <iostream>	
#include <iomanip> 
#include <fstream> 
#include <string>
#include <cstdlib>

using namespace std ;





const int MAX_KENO = 81;

int main() 
{
	int kenoB[MAX_KENO];
	int matchCount;
	int x;
	int ranNum;
	int seed;
	char play;
	int gameCount;
	int userNum[6];
	

	gameCount =1;
	matchCount =0;
	x =0;

	cout << "Welcome to Keno! Would you like to play? Enter 'Y' for Yes and 'N' for No." << endl;
	cin >> play;

	while (play == 'y')
	{

	cout << "Please seed the random number generator by entering a number between 1 and 80>" << endl;
	cin >> seed;
	srand(seed);

	for (x=0; x < MAX_KENO; x++) //works
	{
		kenoB[x]=0;
	}

	cout << "Please enter 6 guesses." << endl; //works
	for (x = 0; x < 6; x++) 
		{
		cin >> userNum[x];
		}



	x=0;
	while (x<20) //works
	{
		ranNum=rand()%80+1;
		if(ranNum !=kenoB[ranNum])
		{
			kenoB[ranNum] = ranNum;
			x++;
		}
	}
	
	


		

	for(x=1; x < 81; x++)
	{
		cout << " "<<kenoB[x] <<" ";
		if(x%10==0)
		{
			cout << endl;
		}

	}
	
	if(userNum[x] == kenoB[ranNum])
		{
		matchCount++;
		}



	cout << "match count is " << matchCount << "     " << gameCount <<endl;

	gameCount ++;
	}
	
return (0);
}


for the life of me I cannot fingure out why it doesnt give me a match count even though I know i have matches. anyone have any ideas/advice? i have been stuck on this for quite a while.
should I use a for loop or something to get the match count?
1
2
if(userNum[x] == kenoB[ranNum]) //x==81 -> crash
        matchCount++;

How do you define a match?
well if one of the random number that were selected is equal to one of the user guess that is a match or what im trying to tell the program
Specific answer:

You're not getting any matches because the probability of the first number you chose being the first random number generated out of however many is staggeringly small. You, as you guessed, need a loop or two to check all your numbers against all the randomly selected numbers.


General advice:

I'd suggest taking advantage of scope and not re-using the same variable all over the place. It's difficult to figure out what x is where and why.

So, instead of
1
2
3
4
5
6
7
8
int x;
...
for (x=0; x<some_thing; x++){...};
...
x=0;
...
if (x==...){...};
...


you'd write
1
2
3
4
5
6
7
8
...
for (int i=0; i<some_thing; i++){...};
...
int x=...;
if (x ==...){...};
...
for (int i=0;i<some_other_thing;i++){...};
...


The difference between this block and the other is that the 'i' goes out of scope and is deleted at the end of the for blocks. You can redeclare and initialize it elsewhere without worrying about losing data or causing problems with names.

Another good plan is to declare AND initialize variables at the same time immediately before they are used for the first time. Otherwise you end up with something like matchcount that gets declared in one place, initialized in another and used way further down without any rhyme or reason to it all.

Also, you really shouldn't litter your program with "magic numbers" -- constants like '80' or '81' whose purpose is indecipherable from context. Like, I have no idea why you're using 80. Is that a standard keno number? Did you pick it out of a hat? What does it even mean? You're better off declaring

const int some_short_but_descriptive_name_without_underscores = 80;

and then using that variable wherever you'd have used your magic numbers. That way it's easier to understand why those numbers are picked, and also easier for you to change them in the future -- all you have to do is change 80 in the declaration to some other number, rather than hunting and pecking through your code to find and change each one.

Take advantage of functions -- it's always a good idea to keep, say, interface stuff separate from actual math. Plus, it lets you reuse names without overlap because they get destroyed thanks to scope!

Oh, and final thing: vectors > arrays for like practically everything. Are there uses for arrays? Sure. Is this one of them? Nope. You'd have a way easier time with a container that self expands, knows its own size and isn't going to transform into a pointer seemingly at random!
You are using kenoB as a bitset (0 if it wasn't choose).
So iterate over userNum, and check if that value is set.
Also, remember to reset kenoB after every garme.
Topic archived. No new replies allowed.