Issue with checking vector struct item is unique

Hi,

I am trying to check to see if my id field is unique. I had the code working when it was just a vector but now that's being used as part of a struct its giving me a weird error (taking me to a pop up page in the compiler called stl_algo.h. Below is my code, would someone please point me in the right direction? Thank you!

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
void getSingleEntry(vector<Record>& address)
{
	
	Record mRecord;
	stringstream ss;
	string id_str;
	
	cout << "Enter Information for fields below, enter ""\"quit""\" to stop entries." << endl;
	
	
	while(true)
	{
		cout << "-----------------" << endl;
    	cout << "NEW ADDRESS ENTRY" << endl;
    	cout << "-----------------" << endl;
    	cout << endl;
    	cout << "Populate the required fields below" << endl;
    	cout << endl;
    	cout << "ID (six digits max): ";
    	getline(cin, mRecord.id);
    	ss << mRecord.id;
    	ss >> id_str;
    	while(mRecord.id.empty() || find(mRecord.id.begin(), mRecord.id.end(), id_str) 
        != mRecord.id.end() || mRecord.id.length() > 6)
    {
        if(mRecord.id.empty())
        {
            cout << "ERROR: ID must be entered, please enter the ID" << endl;
            cout << "ID: ";  
        }
        else if (find(mRecord.id.begin(), mRecord.id.end(), id_str) != mRecord.id.end() )
        {
        	cout << "ERROR: ID must be unique." << endl;
       	 	cout << "ID: ";
            getline(cin, mRecord.id);

            
        }
        else if( mRecord.id.length() > 6)
        {
        	cout << "ERROR: ID can only be six characters, please re-enter." << endl;
       	 	cout << "ID: ";
            getline(cin, mRecord.id);

		}
    }
Last edited on
You have id_str declared as a string pointer: string* id_str;

You are trying to use the find function from the <algorithm> library: find(mRecord.id.begin(), mRecord.id.end(), id_str)

In this case, the find function expects the third parameter to be a string or const char*. id_str is neither.
Last edited on
Thanks for pointing that out was trying a few different ideas and left the pointer there by accident. I still cant get it to work with it being a string data type though, it seems to work fine if it's a standard vector, but when part of a structure its having troubles
My mistake, the 3rd parameter is supposed to be type char in this case. You might want to use the std::string::find function instead.

http://en.cppreference.com/w/cpp/string/basic_string/find
Last edited on
Topic archived. No new replies allowed.