I have a vector personInfo that contains class objects of person details

vector<person> personInfo;

I am trying to use std::find to search for a existing name in the vector.
I overloaded my == operator.

person.h

class person {
    public:
      std::string name;
      friend bool operator==(const person &lhs, const person &rhs);
};
      bool operator==(const person &lhs, const person &rhs) {
           return lhs.name == rhs.name;    

main.cpp

    #include "person.h"
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>

    int main {
        //assuming that I have added stuff into the vector already.

        std::cout << " " << std::endl;
        std::cout << "Enter name to find: ";
        std::cin >> name;

        std::vector<person>::iterator it;
        it = std::find(personInfo.begin(),personInfo.end(),person(name,false));
        if(it !=personInfo.end()) {
             cout << "found!" << endl;
        }
    }

the program compiles successfully but nothing happens even if I enter a existing name that is inside the vector.
I not sure if anything I wrote is wrong. please help. thanks

Your issue may be that std::cin >> name; does not remove the newline char from the resulting string.

Replacing std::cin with the following may work for you :

std::getline( std::cin, name);

Your issue may be that std::cin >> name; does not remove the newline char from the resulting string.

The >> operator is delimited by whitespace, so it would never store a newline unless you go out of your way to make it happen. The problem is more likely that the names are multiple words, and >> only extracts a single word.

Of course, a more complete piece of code that we can actually run would be helpful in troubleshooting. ;)

You could do worse than debugging it yourself. Something like this comes to mind:

 bool operator==(const person &lhs, const person &rhs) 
 {
     cout << "Comparing lhs.name " << lhs.name << " to rhs.name " << rhs.name << endl;
     return lhs.name == rhs.name;  
 }

Ah woops, I misread some documentation on std:cin the language suggested it was the case..

Although it appears they where attempting to describe that the operator only takes the chars to the next whitespace.

If you're correct and the names contain spaces, then using std::getline should resolve the situation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.