Comparing values in const list with values in a vector to find matches

I didn't think I'd run into any problems with this, but..

I'm given a unsigned const int list with 5 numbers, and need to compare it to a vector with 5 numbers as well.


This is giving me an error
 
  if(find(WHITE.begin(), WHITE.end(), playerNumbersWhite.at(i)))


and I also can't get the compiler to recognize the contains function

I've tried a bunch of fixes but I keep getting "expected expression" or errors having to do with the type.

am I not including the right header files or something?
do i need to convert the list into a vector?
Last edited on
Please provide the exact text of the error message.

Also provide the declarations of WHITE and playerNumbersWhite. If possible, give us the whole program so we can try to compile it ourselves.
Google "std::find" and look what is probably the first result, https://en.cppreference.com/w/cpp/algorithm/find

Defined in header <algorithm>


On this page, you'll see the return type for std::find is an iterator.
Return value:
Iterator to the first element satisfying the condition or last if no such element is found.


This translates to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> playerNumbersWhite = {9, 55, 4, 3, 42};
    std::vector<int> WHITE = {1, 2, 3, 4, 5};
    
    for (size_t i = 0; i < playerNumbersWhite.size(); i++)
    {
        if (std::find(WHITE.begin(), WHITE.end(), playerNumbersWhite.at(i)) != WHITE.end())
            std::cout << "Found " << playerNumbersWhite.at(i) << '\n';
    }
}
Last edited on
Since I posted this I've tried to dumb it donw just using an ij nested loop. here's what I've got 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
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
//
//  main.cpp
//  CPT 182_PROJECT 1
//
//  Created by Kevin Kile on 2/28/19.
//  Copyright © 2019 Kevin Kile. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <vector>
#include <list>


using namespace std;


            //Declare functions
unsigned int numWhiteMatch(int num1, int num2, int num3, int num4, int num5);
bool redMatch(int num);

int main() {
    
            //Declare constant variables (Gand prize and winning numbers)
    const unsigned int GRAND_PRIZE = 242000000;
    const unsigned int WHITE[5] = { 2, 12, 16, 29, 54 };
    const unsigned int RED = 6;
    
    

    
            // Verify valid file path
    ifstream fin("/Users/MoKK/School/CPT 182/PROJECT 1/input.txt");
    ofstream fout("/Users/MoKK/School/CPT 182/PROJECT 1/output.txt");
    if (fin.fail()) {
        cout << "Error reading File" << endl;
        cout << system("pause");
        return -1;
    }
    
    vector <unsigned int> playerNumbersWhite(5);
    unsigned int playerNumbersRed;

    
    
    
            //Pull values from input file and populate vector
    
    while (fin >> playerNumbersWhite.at(0) >> playerNumbersWhite.at(1) >> playerNumbersWhite.at(2) >> playerNumbersWhite.at(3) >> playerNumbersWhite.at(4) >> playerNumbersRed) {
        
        numWhiteMatch(playerNumbersWhite.at(0), playerNumbersWhite.at(1), playerNumbersWhite.at(2), playerNumbersWhite.at(3), playerNumbersWhite.at(4));{
            unsigned int numCorrect = 0;
            for (int i = 0; i < 5; ++i) {
                for (int j = 0; j < 5; ++j) {
                    if (playerNumbersWhite.at(i) == WHITE[j]) {
                        numCorrect += numCorrect;
                    }
                
            }
                cout << numCorrect;
                    
                
            }
            
            
            return numCorrect;
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    fout.close();
    fin.close();
    
    
    return 0;
}


But now i'm getting a "clang error" that wont go away, even if I delete all the code...
a simple way is to make a new vector of pairs where value 1 is the value, value 2 is which vector it was in originally. Sort this on value 1. Look for the same value twice or more in a row, if found, check for both vectors being listed, if so, its a match.
I must be missing something with functions. I took out the function and the call and got rid of the clang error
I've gotten the program working as expected now just using a nested loop.

@Jonnin, so what you're saying to do is like using a dictionary in python, right?
Lines 51-63 don't do what you think they do. Line 51 is a call to the function numWhiteMatch(), which you've declared at line 19. At the end of line 51, you start a new block of code within main(). That block executes once.

The error you're getting is that numWhatMatch() isn't defined.

Now here's something funny: just 5 minutes ago I posted in the Lounge section about how this mistake is just too darned easy to make.

Why not pass the vector into numWhiteMatch rather than the individual items?
Line 56: You're basically doubling numCorrect each time.
Topic archived. No new replies allowed.