Reputation: 35
I'm trying to build a function which is supposed to compare to arrays, one generated by itself(the lottery digits) and the second array is submitted and stored by user input.
This is supposed to count the matching digits between the lottery and the ones submitted by the user input. If they match, a message should be displayed telling the user that he has won the big prize ,etc.
This is supposed to be the function that compares both arrays; I assume is right(to make it more clear: The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match)?
int compare(int user[], int lottery[]){
int matches = 0;
for (int i = 0; i < SIZE; i++) {
if (user[i] == lottery[i]) {
matches++;
}
}
return matches;
}
the problem comes when it returns to the main function which is supposed to tell if the user has won or not. Here it is the small block of code that I created in the main function:
int matches = compare(user, lottery);
if (matches) {
cout << "Congratulations, you have won the big prize" << endl;
}
else {
cout << "Please, try again" << endl;
}
The expected should be to display a message if user won and to count the user digits that match with the lottery numbers.
The actual output is the digits entered by the user. Hopefully I could explain myself.
Here is the complete code, if you feel like helping and need a bit of more information. http://cpp.sh/8ivyc
Upvotes: 0
Views: 680
Reputation: 11
Check for inequality rather than equality. You'll return out of the function as soon as an user element not equal to lottery element
int compare(int user[], int lottery[]){
for (size_t i = 0; i < SIZE; i++) {
if (user[i] != lottery[i]) {
return 0;
}
}
return 1;
}
Upvotes: 0
Reputation: 10740
The simplest way is to just use std::array
. If you use std::array
, you can just use ==
to compare them!
The functions populate
and show_values
can be written like this:
void populate(std::array<int, SIZE>& nums) {
srand(time(0));
for (int& value : nums) {
value = rand() % 9;
}
}
void showValues(std::array<int, SIZE>& nums) {
for (int i = 0; i < SIZE; i++) {
cout << setw(3) << nums[i];
}
cout << endl;
}
And compare
is especially simple:
bool compare(std::array<int, Size>& nums1, std::array<int, Size>& nums2) {
return nums1 == nums2;
}
Upvotes: 0
Reputation: 32586
In your case it is enough to have only one time the same value at the same index to match, but all the values must be equals
First possibility with minimal changes from your code :
int compare(int user[], int lottery[]){
int matches = 0;
for (int i = 0; i < SIZE; i++) {
if (user[i] == lottery[i]) {
matches++;
}
}
return (matches == SIZE); /* <<< modified */
}
But it is useless to continue to compare after a different value was found, so can be :
int compare(int user[], int lottery[]){
for (int i = 0; i < SIZE; i++) {
if (user[i] != lottery[i]) {
return 0;
}
}
return 1;
}
and because you are in C++ :
bool compare(int user[], int lottery[]){
for (int i = 0; i < SIZE; i++) {
if (user[i] != lottery[i]) {
return false;
}
}
return true;
}
(also changing the call to use a bool rather than an int to be more clear of course)
Upvotes: 2