Matching numbers are repeating, how do i get rid of that?

closed account (L1USLyTq)
Hi, i am a beginner. I, uh, should do a lottery that asks the user to input 5 numbers and then generate a 5 random numbers for the lotto results then after it should show what numbers matched, and how many numbers matched then shows the user his/her prize.

My problem is in my code, when i debug it, it shows which numbers matched, but it is repeating.

The question:

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 to 9 (see example below) for each array element. The program will compare the corresponding elements in the two arrays and keep a count of the digits that matches. For instance, if the lottery array contains: 8 5 0 2 4 and the user array contains: 5 3 0 8 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 match, display a message proclaiming the user as a grand prize winner. Prizes are as follows:

5 Matching Digits – 500,000.00
4 Matching Digits – 100,000.00
3 Matching Digits – 20,000.00
2 Matching Digits – 500.00
1 Matching Digits – 10.00 (“Balik-Taya”)
0 Matching Digits – Try Again

-----------------------------------
Hint: use rand() for generating random numbers

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{ int x = (rand()%10);
cout << x;}


My code goes like 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// ETO NA TALAGA.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>

using namespace std;

int main()
{
	srand(time(0));

	int userinput[5], lottoresults[5];
	int counter=0;
	char ans=0;

	do{
//intro:
	cout<<"\t\t\n\n";
	cout<<"\t\t\tWelcome to the lucky lotto! \n\n";
	cout<<"\t\t\tLet's get started..\n\n";
	cout<<"\t\t\t  Instruction:\n\n";
	cout<<"\t\t\t  *Input five numbers between 0 and 9 only.\n\n";
	
//inputnum:

	

	for (int i=0; i<5; i++)
		
	{cout<<"\n\n\t\tPlease type your five lucky numbers for today (0-9): ";
	cin>>userinput[i];
	
	for(int a=1; a<=5; a++){ 
		while(userinput[i]==userinput[i-a])
		{cout<<"\n\n\tYou've already entered that number. Please type another number: ";
		 cin>>userinput[i];}
		}
		while(userinput[i]<0||userinput[i]>10)
		{cout<<"\n\n\tEnter numbers between 0-9 only. Please type another number: ";
		cin>>userinput[i];}
	}

	for (int i=0; i<5; i++)

	system("cls");

//results:

	for (int x=0; x<40;x++){
	cout<<"\n\n\t\t\tGenerating numbers.... \n\n";
	cout<<"\n\n\t\t\tPlease wait....\n\n";
	for (int x=0;x<1;x++)
	cout<<" ";};
	system("cls");
	cout<<endl;


	cout<<"\n\n\t\t\tLOTTO WINNING NUMBERS: ";
	for(int lot=0;lot<5; lot++){
	lottoresults[lot]= (rand()%10);
    cout<<lottoresults[lot]<<" ";
	}


	cout<<"\n\n\t\t\tUSER NUMBERS: ";
	for (int i=0; i<5; i++)
	{cout<<userinput[i]<<" ";}

//matcher:

	int matchnum[5];
	for(int x = 0; x < 5; x++)
		for(int y = 0; y < 5; y++)
			if(userinput[x] == lottoresults[y])
				counter++;

	int o=0;
	for(int x = 0; x < 5; x++)
		for(int y = 0; y < 5; y++){
			if(userinput[x] == lottoresults[y]){
				matchnum[0] = userinput[x];
				;o++;}
		}

	cout<<endl;
	cout<<"\n\n\t\t\tMATCHING NUMBERS: ";

	int x = 5;
	for(int y=0; y<5;y++){
		if(y>counter-1)
			break;
			x--;
		cout<<matchnum[0]<<" ";}
		cout<<"\n";


	cout<<endl;
		 if (counter<2)
			 cout<<"\n\n\t\t\tThere are "<<counter<<" matching digit. \n.";
		 else
			 cout<<"\n\n\t\t\tThere are "<<counter<<" matching digits. \n";


	cout<<"\n\n\t\t\tPRIZE: ";
	switch(counter){
	case 0:
		cout<<"Sorry, none of your digits matched.\n";
		cout<<"\t\t\t\tBetter luck next time!\n\n\t\t\t";
		break;
	case 1:
		cout<<"PHP10.00 (Balik-Taya)\n\n";
		break;
	case 2:
		cout<<"PHP500.00.\n\n";
		break;
	case 3:
		cout<<"PHP20,000.\n\n";
		break;
	case 4:
		cout<<"PHP100,000.\n\n";
		break;
	case 5:
		cout<<"All of your digits matched the lotto results.\n";
		cout<<"\t\t\t\tYou won PHP500,000.\n\n";
		break;
	}

	if(counter!= 0)
		for(int x = 0; x < 1; x++){
			cout<<"\n\n\t\t\tCONGRATULATIONS!!\n\n";
			cout<<"\n\n\t\t";
		for(int y = 0; y < 1; y++)
		;}
		system ("pause");
		system("cls");
		

		cout<<endl<<"\n\t\t\tDo you want to play again [Y/N]? ";
		cin>>ans;

		while((ans != 'Y' && ans != 'y')&&(ans != 'N' && ans != 'n')){
			cout<<endl<<"\n\t\tPlease Enter Y or N only!\n\n\t\t";
			cout<<"\t\tDo you still want to play again?: ";
			cin>>ans;

		}
	
		system("CLS");

	}while(ans=='Y'||ans=='y');
	cout<<"\n\n\n\n\n\t\t\tTHANK YOU FOR PLAYING!\n\n\t";
	cout<<"\t\t\Hope you enjoyed.\n\n\t";
	cout<<"\t\tPlay again next time to win for more!\n\n\t\t\t";

cin.sync();
cin.get();
return 0;
}


THANK YOU VERY MUCH FOR THOSE WHO'LL ANSWER, i'm so in need of help, and this needs to be submitted on or before this monday, which is september 9 2013.
Last edited on
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm> // std::random_shuffle
#include <iterator> // std::begin, std::end

int main()
{
	std::srand( std::time(0) ) ;

        // the actual results are the five numbers at the beginning of the array
	int lottoresults[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;

	// generate 5 non-repeating numbers for lottoresults by
	// generating a random permutation of the elements in the array
	// and then picking the first five
	// see: http://en.cppreference.com/w/cpp/algorithm/random_shuffle

	std::random_shuffle( std::begin(lottoresults), std::end(lottoresults) ) ;

	// or: std::random_shuffle( lottoresults, lottoresults+10 ) ; // legacy C++

	// print them out
	for( int i = 0 ; i < 5 ; ++i ) std::cout << lottoresults[i] << ' ' ;
        std::cout << '\n' ;
}
Last edited on
closed account (L1USLyTq)
Thank you very much. But, what i exactly mean is that, the matching numbers is the one that is repeating. When you run the code, you'll see and output like this:

LOTTO WINNING NUMBERS: 6 7 4 2 1
USER INPUT: 5 9 0 8 1
MATCHING NUMBERS: 1 1
PRIZE: PHP20,000
CONGRATULATIONS!!

Try to run the code, and you'll see.
Will you please tell me what code should be replaced in my code? I'm so confused, thank you.
> Will you please tell me what code should be replaced in my code? I'm so confused, thank you.

You need to write a nested loop.

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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm> // std::random_shuffle
#include <iterator> // std::begin, std::end

int main()
{
    const int N = 5 ;
	std::srand( std::time(0) ) ;

    // the actual results are the five numbers at the beginning of the array
	int lottoresults[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;

	// generate 5 non-repeating numbers for lottoresults by
	// generating a random permutation of the elements in the array
	// and then picking the first five
	// see: http://en.cppreference.com/w/cpp/algorithm/random_shuffle
	std::random_shuffle( std::begin(lottoresults), std::end(lottoresults) ) ;

	// or: std::random_shuffle( lottoresults, lottoresults+10 ) ; // legacy C++

	// print them out
	std::cout << "lotto winning numbers: " ;
	for( int i = 0 ; i < N ; ++i ) std::cout << lottoresults[i] << ' ' ;
    std::cout << '\n' ;

    // numbers entered by the user
    int userinput[N] = { 5, 9, 0, 8, 1 } ; // say, as an examplew
	std::cout << "user input: " ;
	for( int i = 0 ; i < N ; ++i ) std::cout << userinput[i] << ' ' ;
    std::cout << '\n' ;

    // check for matching numbers
    int cnt_matches = 0 ; // #matches

    std::cout << "matching numbers: " ;
    for( int i = 0 ; i < N ; ++i ) // for each number entered by the user
    {
        const int user_num = userinput[i] ;

        // check if the number number entered by the user is a winning number
        // (could use std::find() instead of writing a loop of out own)
        for( int j = 0 ; j < N ; ++j ) // for each winning number
        {
            if( user_num == lottoresults[j] ) // is it the same as user_num?
            {
                // yes, we got a match
                ++cnt_matches ;
                std::cout << user_num << ' ' ;
            }
        }
    }

    std::cout << "\nnumber of matches: " << cnt_matches << '\n' ;
}

http://ideone.com/UX3cEZ
Topic archived. No new replies allowed.