array:How do check for duplicate numbers?!

I'm stuck on my lottery game. I got mostly everything to work but the part where the program checks if the user numbers and computer numbers have duplicates in them. I know I'm suppose to use the bool function that I have at the end of my program but I don't know how to incorporate it into the getLottoNum and genWinNum functions. Any sugestions will be greatly appreciated !

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
 # include <iostream>
# include <string>
# include <ctime>
# include <cctype>

using namespace std;

void menuDisplay();
void getLottoNum(int arr[], int size);
void genWinNums( int arr[], int size);
void checkMatches(int arr1[], int arr2[], int size);
void results(string name,int arr1[], int arr2[], int size);
bool isDuplicate(int arr[], int size, int numToCheck);

int main()
{
	const int num = 7;
	int userTicket[num], winningNums[num];
	char choice;
	string name1;

	do
	{
		menuDisplay();
		cin >> choice;
		cout << endl;
		if(choice == '1')
		{
			cout << "Please enter your name: ";
			cin >> name1;
			cout << endl;
						
			getLottoNum(userTicket, num);
			genWinNums(winningNums, num);
			cout << endl;
			results(name1,winningNums, userTicket, num);
		}
		else if(choice =='q'||choice =='Q')
		{
			cout << "You have chosen to quit the program" << endl;
		}
		else if(choice != '1' || choice != 'q' || choice != 'Q')
		{
			cout << "Invalid selection" << endl;
		}
	}while(choice != '1' || choice != 'q' || choice != 'Q');

	system("PAUSE");
	return 0;
}

void menuDisplay()
{
	cout << "LITTLETON CITY LOTTO MODEL:" << endl;
	cout << "---------------------------" << endl;
	cout << "1) Play Lotto" << endl;
	cout << "q) Quit Program" << endl;
	cout << "Please make a selection: " ;
}

void getLottoNum(int arr[] , int size)
{
	cout << "Please enter your 7 lotto number picks between 1 and 40." << endl;

	for(int i = 0; i < size; i++)
	{
		do
		{
			cout << "Selection " << i+1 << ": ";	
			cin >> arr[i];

			if(arr[i] < 1 || arr[i] > 40)
			{
				cout << "The number must be between 1 and 40. Please try again:" << endl; 
			}						
		}while(arr[i] < 1 || arr[i] > 40);
	}
	
}

void genWinNums(int arr[], int size)
{
	srand((unsigned int)time(NULL));
	for(int i = 0 ; i < size ; i++)
	{
		arr[i] = rand() % 40 + 1;
	}
}

void checkMatches(int arr1[], int arr2[], int size)
{
	int match = 0;

	for( int i = 0; i < size ; i ++)
	{

	}
}

void results(string name,int arr1[], int arr2[], int size)
{	
	cout << "" << name << "LOTTO RESULTS" << endl;
	cout << "---------------------------" << endl;
	
	cout << "WINNING TICKET NUMBERS: ";
	for(int i = 0; i < size ; i++)
	{
		cout<<" "<< arr1[i] ;
	}
	cout << endl;
	cout << "" << name << "'S TICKET: ";
	for(int i = 0; i < size ;i++)
	{
		cout << " " << arr2[i] ;
	}
	cout << endl << endl;
	cout << "RESULTS:" << endl;
	cout << "--------" << endl;
	cout << " Number Matches: " << endl;
	cout << "Winnings : " << endl;
	cout << endl;
}

bool isDuplicate(int arr[], int size, int numToCheck)
{
    for(int i = 0;i<size;i++)
{
if(arr[i] = numToCheck)
{
return true;
}
}
return false;
}
Have you tried comparing the loto numbers?
1
2
3
4
5
int loto1= 1234 , loto2 = 1234;
if( loto1 == loto2 )
{
    std::cout << "Loto number 1 is equal to loto number 2!" << std::endl;
}
I tried doing that but it didn't work. I'm saving the numbers directly into the array. I think I'm supposed to save the number into a variable then pass it through the bool function isDuplicate to check. from there I don't know what to do. like do I start another loop ? or state that the numToCheck = arr[] ?
Try sorting it, then seeing if any value is equal to the one next to it.
Const-correct:
1
2
//bool isDuplicate(int arr[], int size, int numToCheck);
bool isDuplicate( const int arr[], int size, int numToCheck ) ;



And if(arr[i] = numToCheck) should be if(arr[i] == numToCheck)
1
2
3
4
5
6
bool isDuplicate( const int arr[], int size, int numToCheck )
{
    for( int i = 0 ; i < size ; ++i ) if( arr[i] == numToCheck ) return true ;
    
    return false ;
}
In fact this code

1
2
3
4
5
6
bool isDuplicate( const int arr[], int size, int numToCheck )
{
    for( int i = 0 ; i < size ; ++i ) if( arr[i] == numToCheck ) return true ;
    
    return false ;
}


corresponds to standard algorithm std::any_of.

To check whether elements of one array are present in another array you can use standard algorithm std::find_first_of.


Topic archived. No new replies allowed.