There is a part of the code:

struct orders { string product; string mark; string surname; string name; string secondName; string phoneNumber; int cost; int dayOfRecept; int monthOfRecept; int yearOfRecept; int dayOfIssue; int monthOfIssue; int yearOfIssue; bool status; }; void fullfillOrdersVector(vector<orders> &Ord) { orders Order; ifstream file("order.txt"); while (file >> Order.product >> Order.mark >> Order.surname >> Order.name >> Order.secondName >> Order.phoneNumber >> Order.cost >> Order.dayOfRecept >> Order.monthOfRecept >> Order.yearOfRecept >> Order.dayOfIssue >> Order.monthOfIssue >> Order.yearOfIssue >> Order.status) { Ord.push_back(Order); } file.close(); } void searchBySurname() { vector <orders> Ord; fullfillOrdersVector(Ord); cout << "Enter the surname/part of it" << endl; string inputString; cin >> inputString; int counter = 0; cout << "Product Mark Owner Phone number Cost Recept Issue Status" << endl; for (int i = 0; i < Ord.size(); i++) { if ((Ord[i].surname).find(inputString)) { cout << i << " " << Ord[i].product << " " << Ord[i].mark << " " << Ord[i].surname << " " << Ord[i].name << " " << Ord[i].secondName << " " << Ord[i].phoneNumber << " " << Ord[i].cost << " " << Ord[i].dayOfRecept << " " << Ord[i].monthOfRecept << " " << Ord[i].yearOfRecept << " " << Ord[i].dayOfIssue << " " << Ord[i].monthOfIssue << " " << Ord[i].yearOfIssue << " " << Ord[i].status << endl; counter++; } } if (counter == 0) { cout << "There're no such surnames in list" << endl; } else { cout << "Users found: " << counter << endl; } } 

The task is to read data from a file into a vector of structures, search for data with the desired last name / part of the last name, and output to the screen. However, when displaying on the screen, I get absolutely the opposite result (that is, everything but the right one is shown). I suspect that the error in the string with (Ord[i].surname).find(inputString) But I do not understand what is wrong

    1 answer 1

    Correctly suspect: Ord[i].surname).find(inputString) will always be true, because std::string::find() returns a string::npos , which is a large number, (not zero) if it fails. Therefore, the check should look like this:

     if ((Ord[i].surname).find(inputString) != string::npos) {...