How to check if a string input is contained in a vector of struct

  c++, if-statement, loops, vector
struct Article
{
int Id;
string name;
string desc;
double price;

Article(int s, string n, string o, double c)
 {
    Id = s;
    name = n;
    desc = o;
    price = c;
 }
};

vector <Article> vArticle;

void GiveName(vector <Article> vArticle);
int main()
{
vector <Article> vArticle;

Article a1(1, "Banana", "Fruit", 7.99);
Article a2(2, "Apple", "Fruit", 5);
Article a3(3, "Book", "Bible", 309.99);
Article a4(4, "Laptop", "Laptop Lenovo", 4989.99);
Article a5(5, "Banana",  "Fruit ", 5.99);

vArticle.push_back(a1);
vArticle.push_back(a2);
vArticle.push_back(a3);
vArticle.push_back(a4);
vArticle.push_back(a5);

GiveName(vArticle);
}
   void GiveName(vector <Article> vArticle)
    {
    vector <Article> vInput;
    cout << "List of articles: " << endl;
    for (int i = 0; i < vArticle.size(); i++)
    {

        cout << vArticle[i].Id << ",  " << vArticle[i].name << ",  " << vArticle[i].desc << ",  " << vArticle[i].price << endl;
    }
    cout << "nnn";
    cout << "Enter an article name" << endl;
    string input;
    cin >> input;
    for (int i = 0; i < vArticle.size(); i++)
      {
        if (vArticle[i].name == input)
        {
            cout << vArticle[i].Id << ",  " << vArticle[i].name << ",  " << vArticle[i].desc << ",  " << vArticle[i].price << endl;
        }
        else
        {
            cout << "Article not found" << endl;
        }
      } }

My code shows "Article not found" for every member that’s not equal to the input, because of using for loop.
What’s the best way to make it show, for example, if i write "Banana", it shows just the elements that contain name Banana.
And if none of the elements contain it, show "Article not found"?

Source: Windows Questions C++

LEAVE A COMMENT