Lottery Ticket program - Need help!

Okay, so my assignment was to "create a program that will generate up to 5 quick pick Lotto Tickets. You must ask the user how many tickets they want and then randomly pick 6 numbers for each ticket between 1 and 50. Remember no ticket can have the same two values on it (i.e. 3, 5 22,34, 34, 50). For 10 bonus points output the tickets in ascending order. The user can buy as many sets of 5 tickets as they would like one set at a time.

Here's what I have so far:
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
//Library Section
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdlib>
#include <time.h>
#include <apmatrix.h>
using namespace std;

//const section

//var section
int Ticket;
char Ans;
int I;
int J;
vector<int>Numbers(5);

	
void main()
{
	srand(time(NULL));
	J=0;

	do{
		cout<<"How many lottery tickets would you like? Please enter a number up to 5:"<<endl;
		cin>>Ticket;

		for(Ticket=0;Ticket<Numbers[I];Ticket++)


		{
		
		for(I=0;I<Numbers.size();I++)
			Numbers[I]=(rand()%50)+1;



		for(I=0;I<Numbers.size();I++)
			cout<<"Lottery ticket:"<<Numbers[I]<<endl;
			


		}
		
		cout<<"Run program again (Y/N)?"<<endl;
		cin>>Ans;



	}while(Ans=='y'||Ans=='Y');


}//end main 


What I still need help on is with the code to create the numbers, stop the program from creating duplicates, and the ascending order part would be nice :)
As of right now, the code does not even run correctly! Help would be much appreciated.
The generating of the random numbers along with the prevent duplicates goes hand in hand. Check this logic:
make a dynamic array (eg std::vector)
while you have less numbers than you need
    make a random number
    check to see if you already have the number (eg with std::find)
        if you did not find the number, add it


Use srand(time(0)) at the start of main and never again, then use rand() to get a random number.

For the bonus, you can use std::sort on your std::vector (or even simple array) and it will sort the array for you ;)

std::vector -> http://www.cplusplus.com/reference/stl/vector/
std::find -> http://www.cplusplus.com/reference/algorithm/find/
std::sort -> http://www.cplusplus.com/reference/algorithm/sort/
Last edited on
Okay, I tried the codes but my teacher told me not to use commands that we haven't used in class :(

I've got an improved version but it still doesn't work, can you help me with what might be wrong?

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
//var section
vector<int>Ticket(5);
char Ans;
int First;
int Second;
int Third;
vector<int>Numbers(6);
int N;
int T;
bool I;
int F;

void main()
{
	srand(time(NULL));
	First=0;
	Second=0;
	Third=0;

	do{ //main do while loop

		system("cls");

		do{ //Do while for tickets
		cout<<"How many lottery tickets would you like? Please enter a number up to 5:"<<endl;
		cin>>N;

			if(N<1||N>5)
			cout<<"Please enter a valid number of tickets."<<endl;
		}while(N<1||N>5);

		for(F=0;F<N;F++) //Main for loop
		{
		
				
			for(Third=0;Third<6;Third++)  //Create randomness
			{		
						Numbers[Third]=((rand()%50)+1);
						
						do{ //duplicate prevention
							I=false;

							for(Second=0;Second<Third;Second++)
							{
								if(Numbers[Second]==Numbers[First])
								{
									I=true;
									Numbers[First]=((rand()%50)+1);
									
								}
							}

						  }while(I==true);
			}
					

				for(First=0;First<Numbers.size();First++)
				{
					for(Second=First+1;Second<Numbers.size();Second++)

					{ //least to greatest

						if(Ticket[First]>=Ticket[Second])
						{
							T=Ticket[First];
							Ticket[First]=Ticket[Second];
							Ticket[Second]=T;
						}
					}
				}//ends the least to greatest


				for(Third=0;Third<Numbers.size();Third++) //Displays the numbers
					{	
						
						
						cout<<Numbers[Third]<<"  ";

				
					}
				cout<<endl;
			
		}



		cout<<"Run program again (Y/N)?"<<endl;
		cin>>Ans;



	   }while(Ans=='y'||Ans=='Y');


}//end main 
1
2
3
4
5
if(Numbers[Second]==Numbers[First])  //It looks like First = 0 and never changes...
{
   I=true;
   Numbers[First]=((rand()%50)+1);  //Using first again...
}
I made a powerball program that do the numbers. No duplicates. I tried to copy the whole program in here wit the quicksor, printout, time stamp files and the whole nine yards but was to big to fit. If you want it, send me an e-mail and I will send you the file. perhojfeldt@gmail.com. Works great by the way. Have won a couple of times. Pure luck. Ha Ha. Here is the randomizing part of it with no duplicates generated.



void CreateRandom(void)
{
/* Setup a random seed number from computer time */

srand(time(NULL));

/* Get random number */

do {

uiSame = 0;

/* Fill buffer with random numbers between 1 and 59 */

for (uiCnt = 0; uiCnt < 5; uiCnt++) {

uiBufNumber[uiCnt] = (1 + rand()) % 60;
}

/* Test for any numbers are the same. Redo if numbers are the same. */

for (uiCntA = 4; uiCntA > 0; uiCntA--) {

for (uiCnt = 0; uiCnt < uiCntA; uiCnt++) {

if (uiBufNumber[uiCnt] == uiBufNumber[uiCntA]) {

uiSame = 1;
}
}
}

} while (uiSame != 0);
}

I don't know why it don't do the indent. They are all spaces. Weird.
Last edited on
Here is another way to do the random number. Do it like they do on TV.

#1. Make a random number (RN) between 1 and 50.

Save it in bin 1 (B1).

Do #1.

#2. Compare RN with B1.

If different save in bin 2 (B2)

else do #1 and goto #2.

and so on.

The different with what I did in the code is I did all random numbers at once and checked of any was the same and if they were did it all over again until they are all different. This will this longer time (ms) because the chance of getting 2 numbers alike out of 5 is higher than 2 numbers out of 2. Hopes this help.



I wrote the code and checked it. works fine. Create random number between 1 and 50 where none are the same. Only thing missing is the quick sort and that it. Her you go.


void CreateRandom_1(void)
{

int iPointer;
int iRandomNumber[5];

/* Setup a random seed number from computer time */

srand(time(NULL));

/* Fill random bin numbers with all zeroes */

for (iPointer = 0; iPointer < 5; iPointer++) {

iRandomNumber[iPointer] = 0;
}

/* Get random numbers 1 to 5 between 1 and 50 */

for (iPointer = 0; iPointer < 5; iPointer++) {

/* Check for numbers are the same. If so redo */

do {

iRandomNumber[iPointer] = ((rand() % 50) + 1);

} while (iRandomNumber[iPointer] == (iRandomNumber[0] || iRandomNumber[1] ||
iRandomNumber[2] || iRandomNumber[3]));
}
}
Here is the quicksort. Place the numbers from lowest to highest in iRandomNumber[0-4]'


/* Quick sort the numbers */

qsort(iRandomNumber, 5, sizeof(iRandomNumber[0]), strcmp);
Topic archived. No new replies allowed.