Lottery C++ Help

.) Lotto is a game in which winners are chosen by random drawing of numbers from among those who have paid money to participate. Write a program that will simulate the lotto game by asking the user five numbers(use array) and should generate a random number in the range of 0-9 for each array element. The program will compare the corresponding elements in the two arrays and if a count of the digits that matches. For instance, if the lottery array contains 8,5,0,2, and 4 and the user array contains 5,3,0,8 and 4, we can say that there are three numbers that matches. The program should display the random numbers stored in the lotto array and the matching digits. If all of the digits matched, display a message proclaiming the user as a grand prize winner. Prizes are as follows:
5 matching digits- 500,000 pesos
4 matching digits-100,000 pesos
3 matching digits- 20,000 pesos
2 matching digits- 500 pesos
1 matching digits-10 pesos or Balik taya! ~(>3>)~
0 matching digits-Try Again!~(^3>)~

I use this code:
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;


int main()
{
int WN[5];
int UI[5];
int MN = 0;

WN[0] = (rand() % 10);
WN[1] = (rand() % 10);
WN[2] = (rand() % 10);
WN[3] = (rand() % 10);
WN[4] = (rand() % 10);

cout << "LOTTO Winning Numbers: " << WN[0] << WN[1] << WN[2] << WN[3] << WN[4] << endl;

cout << "1st USER Number: ";
cin >> UI[0];
cout << "2nd USER Number: ";
cin >> UI[1];
cout << "3rd USER Number: ";
cin >> UI[2];
cout << "4th USER Number: ";
cin >> UI[3];
cout << "5th USER Number: ";
cin >> UI[4];

cout << "Matching Numbers: ";
if (WN[0] == UI[0]){
MN = MN + 1;
cout << WN[0];
}
if (WN[0] == UI[1]){
MN = MN + 1;
cout << WN[0];
}
if (WN[0] == UI[2]){
MN = MN + 1;
cout << WN[0];
}
if (WN[0] == UI[3]){
MN = MN + 1;
cout << WN[0];
}
if (WN[0] == UI[4]){
MN = MN + 1;
cout << WN[0];
}
if (WN[1] == UI[0]){
MN = MN + 1;
cout << WN[1];
}
if (WN[1] == UI[1]){
MN = MN + 1;
cout << WN[1];
}
if (WN[1] == UI[2]){
MN = MN + 1;
cout << WN[1];
}
if (WN[1] == UI[3]){
MN = MN + 1;
cout << WN[1];
}
if (WN[1] == UI[4]){
MN = MN + 1;
cout << WN[1];
}
if (WN[2] == UI[0]){
MN = MN + 1;
cout << WN[2];
}
if (WN[2] == UI[1]){
MN = MN + 1;
cout << WN[2];
}
if (WN[2] == UI[2]){
MN = MN + 1;
cout << WN[2];
}
if (WN[2] == UI[3]){
MN = MN + 1;
cout << WN[2];
}
if (WN[2] == UI[4]){
MN = MN + 1;
cout << WN[2];
}
if (WN[3] == UI[0]){
MN = MN + 1;
cout << WN[3];
}
if (WN[3] == UI[1]){
MN = MN + 1;
cout << WN[3];
}
if (WN[3] == UI[2]){
MN = MN + 1;
cout << WN[3];
}
if (WN[3] == UI[3]){
MN = MN + 1;
cout << WN[3];
}
if (WN[3] == UI[4]){
MN = MN + 1;
cout << WN[3];
}
if (WN[4] == UI[0]){
MN = MN + 1;
cout << WN[4];
}
if (WN[4] == UI[1]){
MN = MN + 1;
cout << WN[4];
}
if (WN[4] == UI[2]){
MN = MN + 1;
cout << WN[4];
}
if (WN[4] == UI[3]){
MN = MN + 1;
cout << WN[4];
}
if (WN[4] == UI[4]){
MN = MN + 1;
cout << WN[4];
}

if (MN = 5);
cout << endl << "Prize: P500,000. Congratulations! You one lucky son of a God!" << endl;
if (MN = 4);
cout << endl << "Prize: P100,000. Congratulations! The odds is in your favor!"<<endl;
if (MN = 3);
cout << endl << "Prize: P20,000. Congratulations! You can now buy an expensive item of your choice!" << endl;
if (MN = 2);
cout << endl << "Prize: P500. Yay! Added allowance!" << endl;
if (MN = 1);
cout << endl << "Prize: P10. Balik taya!" << endl;
if (MN = 0);
cout << endl << "Zero Matching Digits. Try Again.";


system("pause>0");
return 0;
}
//i'm not done yet but i have a problem in the prize part... :/
Output of my program:
LOTTO Winning Numbers: 17409
Enter 1st number: 1
Enter 2nd number: 3
Enter 3rd number: 4
Enter 4th number: 6
Enter 5th number: 7
Matching Number: 174
Prize: Prize: P500,000. Congratulations! You one lucky son of a God!
Prize: P100,000. Congratulations! The odds is in your favor!
Prize: P20,000. Congratulations! You can now buy an expensive item of your choice!
Prize: P500. Yay! Added allowance!
Prize: P10. Balik taya!
Zero Matching Digits. Try Again.
UPDATE:
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;


int main()
{
int WN[5];
int UI[5];
int MN = 0;

WN[0] = (rand() % 10);
WN[1] = (rand() % 10);
WN[2] = (rand() % 10);
WN[3] = (rand() % 10);
WN[4] = (rand() % 10);

cout << "LOTTO Winning Numbers: " << WN[0] << WN[1] << WN[2] << WN[3] << WN[4] << endl;

cout << "1st USER Number: ";
cin >> UI[0];
cout << "2nd USER Number: ";
cin >> UI[1];
cout << "3rd USER Number: ";
cin >> UI[2];
cout << "4th USER Number: ";
cin >> UI[3];
cout << "5th USER Number: ";
cin >> UI[4];

cout << "Matching Numbers: ";
if (WN[0] == UI[0]){
MN = MN + 1;
cout << WN[0];
}
if (WN[0] == UI[1]){
MN = MN + 1;
cout << WN[0];
}
if (WN[0] == UI[2]){
MN = MN + 1;
cout << WN[0];
}
if (WN[0] == UI[3]){
MN = MN + 1;
cout << WN[0];
}
if (WN[0] == UI[4]){
MN = MN + 1;
cout << WN[0];
}
if (WN[1] == UI[0]){
MN = MN + 1;
cout << WN[1];
}
if (WN[1] == UI[1]){
MN = MN + 1;
cout << WN[1];
}
if (WN[1] == UI[2]){
MN = MN + 1;
cout << WN[1];
}
if (WN[1] == UI[3]){
MN = MN + 1;
cout << WN[1];
}
if (WN[1] == UI[4]){
MN = MN + 1;
cout << WN[1];
}
if (WN[2] == UI[0]){
MN = MN + 1;
cout << WN[2];
}
if (WN[2] == UI[1]){
MN = MN + 1;
cout << WN[2];
}
if (WN[2] == UI[2]){
MN = MN + 1;
cout << WN[2];
}
if (WN[2] == UI[3]){
MN = MN + 1;
cout << WN[2];
}
if (WN[2] == UI[4]){
MN = MN + 1;
cout << WN[2];
}
if (WN[3] == UI[0]){
MN = MN + 1;
cout << WN[3];
}
if (WN[3] == UI[1]){
MN = MN + 1;
cout << WN[3];
}
if (WN[3] == UI[2]){
MN = MN + 1;
cout << WN[3];
}
if (WN[3] == UI[3]){
MN = MN + 1;
cout << WN[3];
}
if (WN[3] == UI[4]){
MN = MN + 1;
cout << WN[3];
}
if (WN[4] == UI[0]){
MN = MN + 1;
cout << WN[4];
}
if (WN[4] == UI[1]){
MN = MN + 1;
cout << WN[4];
}
if (WN[4] == UI[2]){
MN = MN + 1;
cout << WN[4];
}
if (WN[4] == UI[3]){
MN = MN + 1;
cout << WN[4];
}
if (WN[4] == UI[4]){
MN = MN + 1;
cout << WN[4];
}

if (MN = 5)
cout << endl << "Prize: P500,000. Congratulations! You one lucky son of a God!" << endl;
else if (MN = 4)
cout << endl << "Prize: P100,000. Congratulations! The odds is in your favor!"<<endl;
else if (MN = 3)
cout << endl << "Prize: P20,000. Congratulations! You can now buy an expensive item of your choice!" << endl;
else if (MN = 2)
cout << endl << "Prize: P500. Yay! Added allowance!" << endl;
else if (MN = 1)
cout << endl << "Prize: P10. Balik taya!" << endl;
else if (MN = 0)
cout << endl << "Zero Matching Digits. Try Again.";


system("pause>0");
return 0;
}
OUTPUT PROGRAM:
LOTTO Winning Numbers: 17409
Enter 1st number: 1
Enter 2nd number: 3
Enter 3rd number: 4
Enter 4th number: 6
Enter 5th number: 7
Matching Number: 174
Prize: Prize: P500,000. Congratulations! You one lucky son of a God!
ok i figured out everything i just need to know how to get the prize?
you seem to be doing a hell of a lot of the same thing e.g.
MN = MN + 1;

(which is MN+=1, or MN++).

If you used a for loop and iterated over the array your code would shrink down a hell of a lot.
Last edited on
how... i'm just a 1st year student of c++ ;.; and my prof is shit lol
example:

1
2
3
4
5
6
7
8
9
10
for(int i=0;i<5;i++)
{
    for(int j= 0;j<5;j++)
    {
        if (WN[i] == UI[j])
        {
             MN++;
        }
     }
}


I haven't checked exactly what indices from which arrays you are comparing, but it's this kind of idea. I think :)
int main()
{
int WN[5];
int UI[5];
int MN = 0;

srand(time(NULL));
WN[0] = (rand() % 10);
WN[1] = (rand() % 10);
WN[2] = (rand() % 10);
WN[3] = (rand() % 10);
WN[4] = (rand() % 10);

cout << "LOTTO Winning Numbers: " << WN[0] << WN[1] << WN[2] << WN[3] << WN[4] << endl;
cout << "USER Numbers: " << endl;
for (int i = 0; i < 5; i++)
{
cout << "Enter a digit between 0 and 9: ";
cin >> UI[i];
while (UI[i]<0 || UI[i]>9)
{
cout << "Error! Entry must be between 0 and 9: ";
cin >> UI[i];
}
}

for (int i = 0; i<5; i++)
{
for (int j = 0; j<5; j++)
{
if (WN[i] == UI[j])
{
MN++;
}
}
}



if (MN = 5)
cout << endl << "Prize: P500,000. Congratulations! You one lucky son of a God!" << endl;
else if (MN = 4)
cout << endl << "Prize: P100,000. Congratulations! The odds is in your favor!" << endl;
else if (MN = 3)
cout << endl << "Prize: P20,000. Congratulations! You can now buy an expensive item of your choice!" << endl;
else if (MN = 2)
cout << endl << "Prize: P500. Yay! Added allowance!" << endl;
else if (MN = 1)
cout << endl << "Prize: P10. Balik taya!" << endl;
else if (MN = 0)
cout << endl << "Zero Matching Digits. Try Again.";


}
system("pause>0");
return 0;
}
//error ;o
int main()
{
int WN[5];
int UI[5];
int MN = 0;

srand(time(NULL));
WN[0] = (rand() % 10);
WN[1] = (rand() % 10);
WN[2] = (rand() % 10);
WN[3] = (rand() % 10);
WN[4] = (rand() % 10);

cout << "LOTTO Winning Numbers: " << WN[0] << WN[1] << WN[2] << WN[3] << WN[4] << endl;
cout << "USER Numbers: " << endl;
for (int i = 0; i < 5; i++)
{
cout << "Enter a digit between 0 and 9: ";
cin >> UI[i];
while (UI[i]<0 || UI[i]>9)
{
cout << "Error! Entry must be between 0 and 9: ";
cin >> UI[i];
}
}
cout << "Matching Numbers: ";
for (int i = 0; i<5; i++)
{
for (int j = 0; j<5; j++)
{
if (WN[i] == UI[j])
cout << WN[i];
{
MN++;
}
}
}



if (MN = 5)
cout << endl << "Prize: P500,000. Congratulations! You one lucky son of a God!" << endl;
else if (MN = 4)
cout << endl << "Prize: P100,000. Congratulations! The odds is in your favor!" << endl;
else if (MN = 3)
cout << endl << "Prize: P20,000. Congratulations! You can now buy an expensive item of your choice!" << endl;
else if (MN = 2)
cout << endl << "Prize: P500. Yay! Added allowance!" << endl;
else if (MN = 1)
cout << endl << "Prize: P10. Balik taya!" << endl;
else if (MN = 0)
cout << endl << "Zero Matching Digits. Try Again.";


system("pause>0");
return 0;
}
i fixed it and the only problem is the prize
= is assignment. == is comparison.
did that but still the prize does not output :(
In:

1
2
3
4
5
6
    if ( WN[i] == UI[j] )
        cout << WN[i] ;

    {
        MN++ ;
    }


line 5 is not governed by the if condition. It is executed unconditionally, so MN will always be 25 after your nested loops. It would have been easier to spot if you'd bothered researching how to use code tags on this site. ;)

http://www.cplusplus.com/articles/jEywvCM9/
what should i do to fix it?? :P
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
int main()
{
	int WN[5];
	int UI[5];
	int MN = 0;

	srand(time(NULL));
	WN[0] = (rand() % 10);
	WN[1] = (rand() % 10);
	WN[2] = (rand() % 10);
	WN[3] = (rand() % 10);
	WN[4] = (rand() % 10);

	cout << "LOTTO Winning Numbers: " << WN[0] << WN[1] << WN[2] << WN[3] << WN[4] << endl;
	cout << "USER Numbers: " << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "Enter a digit between 0 and 9: ";
		cin >> UI[i];
		while (UI[i]<0 || UI[i]>9)
		{
			cout << "Error! Entry must be between 0 and 9: ";
			cin >> UI[i];
		}
	}
	cout << "Matching Numbers: ";
	for (int i = 0; i<5; i++)
	{
		for (int j = 0; j<5; j++)
		{
			if (WN[i] == UI[j]){
				cout << WN[i]<<MN;
				MN++;
			}
			{
				if (MN == 5)
					cout << endl << "Prize: P500,000. Congratulations! You one lucky son of a God!" << endl;
				else if (MN == 4)
					cout << endl << "Prize: P100,000. Congratulations! The odds is in your favor!" << endl;
				else if (MN == 3)
					cout << endl << "Prize: P20,000. Congratulations! You can now buy an expensive item of your choice!" << endl;
				else if (MN == 2)
					cout << endl << "Prize: P500. Yay! Added allowance!" << endl;
				else if (MN == 1)
					cout << endl << "Prize: P10. Balik taya!" << endl;
				else if (MN == 0)
					cout << endl << "Zero Matching Digits. Try Again.";
			}
		}
	}
	


system("pause>0");
return 0;
}
copy his code and replace yours
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
#include<iostream>
#include<cstdlib>
#include <windows.h>
#include<ctime>
using namespace std;


int main()
{
int WN[5];
int UI[5];
int MN = 0;

WN[0] = (rand() % 10);
WN[1] = (rand() % 10);
WN[2] = (rand() % 10);
WN[3] = (rand() % 10);
WN[4] = (rand() % 10);

cout << "LOTTO Winning Numbers: " << WN[0] << WN[1] << WN[2] << WN[3] << WN[4] << endl;

cout << "1st USER Number: ";
cin >> UI[0];
cout << "2nd USER Number: ";
cin >> UI[1];
cout << "3rd USER Number: ";
cin >> UI[2];
cout << "4th USER Number: ";
cin >> UI[3];
cout << "5th USER Number: ";
cin >> UI[4];

cout << "Matching Numbers: ";
for(int i=0;i<5;i++)
{
    for(int j= 0;j<5;j++)
    {
        if ( WN[i] == UI[j] )
        cout << WN[i] ;
        {
        MN++ ;
        }
     }
}

if (MN = 5)
cout << endl << "Prize: $500,000. Congratulations! You one lucky son of a Human!" << endl;
else if (MN = 4)
cout << endl << "Prize: $100,000. Congratulations! The odds is in your favor!"<<endl;
else if (MN = 3)
cout << endl << "Prize: $20,000. Congratulations! You can now buy an expensive item of your choice!" << endl;
else if (MN = 2)
cout << endl << "Prize: $500. Yay! Added allowance!" << endl;
else if (MN = 1)
cout << endl << "Prize: $10. Ten Dollars!" << endl;
else if (MN = 0)
cout << endl << "Zero Matching Digits. Try Again.";


system("pause>0");
return 0;
}
Last edited on
that's my code... loool
i just sent my program with all the errors :v I DECLARE THIS THREAD CLOSED BUT NOT SOLVED.
Topic archived. No new replies allowed.