每个项目的C 字符串数组搜索输出

C++ String Array search outputs for every item

本文关键字:数组 搜索 输出 字符串 项目      更新时间:2023-10-16

我知道这有点令人困惑,但我确实需要帮助。我需要在数组中找到一个带有许多字符串的字符串。如果找不到字符串,则显示适当的消息。但是,当我用于循环时,它显示了阵列中每个字符串的此消息,尽管它也显示了找到的字符串...我希望您理解我的意思,如果我没有意义,则很抱歉。这是我的代码:

void Store::search() {
        string name;
        cout << "Enter name of product you're searching: " << endl;
        getline(cin, name);
        for (int i = 0; i < quantity; i++) {
            if (name.compare(database[i].name) == 0){                           
            cout << "-------------<Product found!>-------------" << endl;
            cout << "name: " << database[i].name << endl;
            cout << "supplier: " << database[i].supplier << endl;
            cout << "available quantity: " << database[i].quantity<< endl;
            cout << "price per unit: " << database[i].price<< endl;
            cout << "------------------------------------------" << endl;
                }
            else
            {
                cout << "Product doesn't exist in database!" << endl;
            }
          }     
        }   

代码可用于搜索,但是如何停止输出"数据库中不存在的产品!"对于未找到的数组中的每个项目(即使找到了搜索的项目)?

预先感谢您

您可以使用语句标志:

void Store::search() 
{
    string name;
    bool found = false
    cout << "Enter name of product you're searching: " << endl;
    getline(cin, name);
    for (int i = 0; i < quantity; i++) 
    {
        if (name.compare(database[i].name) == 0){                           
        cout << "-------------<Product found!>-------------" << endl;
        cout << "name: " << database[i].name << endl;
        cout << "supplier: " << database[i].supplier << endl;
        cout << "available quantity: " << database[i].quantity<< endl;
        cout << "price per unit: " << database[i].price<< endl;
        cout << "------------------------------------------" << endl;
        found = true;
        break;
    }
    if (!found)
       cout << "Product doesn't exist in database!" << endl;
}   

您也可以使用std :: find_if,这将使您的代码看起来像:

auto it = std::find_if(databases.begin(), databases.end(), [&name](const auto &database) {return name.compare(database.name) == 0; });
if (it != databases.end())
{
    cout << it->name << endl;
    cout << "found" << endl;
}
else
{
    cout << "not found" << endl;
}

一般而言,C 提供了许多此类功能,这些功能通常会使您的代码缩短,提高可读性和保证功能

您可以:
1.如果在for loop中找到该项目,请保持Bool变量以设置为true
2.在找到项目
时,添加break立即退出循环 3.删除其他部分,因为它将打印出"数据库中不存在的产品!"对于每个循环周期,如果项目不匹配
4. for循环后,检查 found是否为false以检查集合中是否不存在

中的项目。
bool found = false;
for (int i = 0; i < quantity; i++)
{
    if (name.compare(database[i].name) == 0)
    {
        cout << "-------------<Product found!>-------------" << endl;
        cout << "name: " << database[i].name << endl;
        cout << "supplier: " << database[i].supplier << endl;
        cout << "available quantity: " << database[i].quantity<< endl;
        cout << "price per unit: " << database[i].price<< endl;
        cout << "------------------------------------------" << endl;
        found = true; // set "found" to true
        break; // add a break to immediately exit for loop when item is found
    }
  }
if (!found)
{
    cout << "Product doesn't exist in database!" << endl;
}

我假设您想在数据库中搜索产品,并在找到其详细信息中。否则,您想通知用户找不到该产品。如果我正确理解您,那么您需要将其他语句从" for"循环中移出,例如:

void Store::search() {
    string name;
    cout << "Enter name of product you're searching: " << endl;
    getline(cin, name);
    bool found = false;
    for (int i = 0; i < quantity; i++) {
        if (name.compare(database[i].name) == 0){                           
        cout << "-------------<Product found!>-------------" << endl;
        cout << "name: " << database[i].name << endl;
        cout << "supplier: " << database[i].supplier << endl;
        cout << "available quantity: " << database[i].quantity<< endl;
        cout << "price per unit: " << database[i].price<< endl;
        cout << "------------------------------------------" << endl;
        found = true;
        break;
        }
    }     
    if (!found)
    {
        cout << "Product doesn't exist in database!" << endl;
    }
} 

如果您的数据库可能包含更多具有相同名称的产品,请删除" Break;";语句。

一种更"现代的C "方法是利用C 算法(例如STD :: find_if),Lambdas和Auto Specifier。

为例(假设数据库是std ::向量或某种STL容器):

auto it = std::find_if(database.begin(), database.end(), [&name](const auto& item) { return name.compare(item.name) == 0; });
if (it != database.end())
{
    cout << it->name << endl;
    cout << "found" << endl;
}
else
{
    cout << "not found" << endl;
}