[try Beta version]
Not logged in

 
Help, how to check lotto ticket's numbers ?

Nov 1, 2015 at 12:41am
Can some one tell me what's wrong with my code? I am generating tickets and would like to compare each ticket number to the wining numbers and then display the results of how many wins ( 1 number matching, two matching etc) but my code doesn't do that.

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
  for (int i = 0; i < ticknum; i++) // generates tickets
	{

		n1 = 1 + (rand() % 47);
		n2 = 1 + (rand() % 47);
		n3 = 1 + (rand() % 47);
		n4 = 1 + (rand() % 47);
		n5 = 1 + (rand() % 47);
		n6 = 1 + (rand() % 27);
		if (n1 != n2 && n1 != n3 && n1 != n4 && n1 != n5 &&
			n2 != n3 && n2 != n4 && n2 != n5 &&
			n3 != n4 && n3 != n5 && n4 != n5)
		{

			//cout << "Ticket #" << i << ' ' << n1 << " " << n2 << " " << n3 << " " << n4 << " " << n5 << " " << n6 << endl;
		}


		
	}

	for (int i = 0; i < 6 ; i++) // checks tickets wins
	{
		megacount = false;
		win_count = 0;

		
		
			if (n1 == 17 || n1 == 21 || n1 == 27 || n1 == 30 || n1 == 47)
			{
				win_count++;
}
			if (n2 == 17 || n2 == 21 || n2 == 27 || n2 == 30 || n2 == 47)
			{
				win_count++;

and so on for each number,do I need to put if else after the first if and also if someone can help me set my loop to check each ticket number that would be great, I set the loop for the ticktets but I realized that it should loop based on each number on the ticket but I don't know how to go about. Thanks.
Nov 1, 2015 at 3:05am
This is one of the ways you could generate winning numbers. Your loop to query user for numbers and test if they match will be much the same as 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
#include	<iostream>
#include	<vector>
#include	<ctime>

using namespace std;

int main (void) {
	vector <unsigned> Numbs;
	
	srand ( time (NULL) );				// Seed random number generator
	
	// Select five winning numbers in draw.
	for (int x = 0; x < 5; x++) {
		unsigned Value;
		
	Another:
		Value = rand () % 47 + 1;		// Pick a winning number
		
		/* Determine if there is a duplicate with those already selected */
		for ( auto V : Numbs )
			
			/* This is probably not required, but could happen */
			if ( V == Value )
				goto Another;

		Numbs.push_back (Value);		// Save new value in array.
		cout << "[" << Value << "]  ";	// Display to prove algo works.
	}
	
	return 0;
}
Nov 1, 2015 at 3:36am
I have already generated tickets in my code, now I want to check those tickets and see if any of their numbers match the winning numbers.
Nov 1, 2015 at 4:24am
This method although not wrong, introduces an unnecessary level of complexity.

1
2
3
4
5
6
		n1 = 1 + (rand() % 47);
		n2 = 1 + (rand() % 47);
		n3 = 1 + (rand() % 47);
		n4 = 1 + (rand() % 47);
		n5 = 1 + (rand() % 47);
		n6 = 1 + (rand() % 27);

However, if this is what you choose then a viable option may be;

1
2
    if ( Check_Number (selection))
        cout << selection << " matches" << endl;

In the function "Check_Number" you could implement something like in lines 10 - 12 in your example.
Topic archived. No new replies allowed.