我知道这个题目有些令人困惑,但我真的需要帮助。 我需要在数组中找到一个包含许多字符串的字符串。如果找不到该字符串,则显示相应的消息。但是当我使用for循环时,它会为数组中的每个字符串显示此消息,虽然它也显示找到的字符串...但我希望你能理解我的意思和抱歉,如果我没有意义的话。这是我的代码:
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;
}
}
}
该代码适用于搜索,但如何停止输出“产品在数据库中不存在!”对于未找到的数组中的每个项目(即使找到搜索项目)?
提前谢谢
答案 0 :(得分:2)
您可以使用语句标记:
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;
}
答案 1 :(得分:2)
您还可以使用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 ++提供了许多这样的功能,通常可以缩短代码,提高可读性并保证功能
答案 2 :(得分:1)
你可以:
1.如果在for循环中找到该项,则将bool变量设置为true
2.添加break
以便在找到项目时立即退出循环
3.删除else部分,因为它会打印出“产品在数据库中不存在!”如果项目不匹配,则为每个循环周期
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;
}
答案 3 :(得分:1)
我假设您要搜索数据库中的产品并在找到时打印其详细信息。否则,您希望通知用户未找到该产品。如果我理解正确,那么你需要将'else语句移出&#39;对于&#39;循环,例如:
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;
}
}
如果您的数据库可能包含更多具有相同名称的产品,请删除&#39; break;&#39;言。
答案 4 :(得分:0)
更“现代的C ++”方法是利用C ++算法(例如std::find_if),lambdas以及auto说明符。
例如(假设数据库是std :: vector或某种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;
}