C++ C++;每个项目的字符串数组搜索输出
我知道这个题目有点让人困惑,但我真的需要帮助。 我需要在包含许多字符串的数组中找到一个字符串。如果未找到字符串,则会显示相应的消息。然而,当我使用for循环时,它会为数组中未找到的每个字符串显示此消息,尽管它也会显示已找到的字符串。。。我希望你明白我的意思,如果我说不通,请原谅。这是我的密码:C++ C++;每个项目的字符串数组搜索输出,c++,arrays,string,compare,C++,Arrays,String,Compare,我知道这个题目有点让人困惑,但我真的需要帮助。 我需要在包含许多字符串的数组中找到一个字符串。如果未找到字符串,则会显示相应的消息。然而,当我使用for循环时,它会为数组中未找到的每个字符串显示此消息,尽管它也会显示已找到的字符串。。。我希望你明白我的意思,如果我说不通,请原谅。这是我的密码: void Store::search() { string name; cout << "Enter name of product you're search
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存储::搜索(){
字符串名;
cout您可以使用语句标志:
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;
}
void存储::搜索()
{
字符串名;
bool found=false
cout您可以使用语句标志:
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;
}
void存储::搜索()
{
字符串名;
bool found=false
你可以:
1.如果在for循环中找到项目,则将bool变量设置为true
2.添加一个break
以在找到项目时立即退出for循环
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;
}
boolfound=false;
对于(int i=0;i
1.如果在for循环中找到项目,则将bool变量设置为true
2.添加一个break
以在找到项目时立即退出for循环
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;
}
boolfound=false;
对于(int i=0;i cout我假设您希望在数据库中搜索产品,并在找到时打印其详细信息。否则,您希望通知用户未找到该产品。如果我理解正确,则您需要将else语句移出“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;
}
}
void存储::搜索(){
字符串名;
cout我假设您希望在数据库中搜索产品,并在找到时打印其详细信息。否则,您希望通知用户未找到该产品。如果我理解正确,则您需要将else语句移出“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;
}
}
void存储::搜索(){
字符串名;
cout您还可以使用,这将使您的代码看起来像:
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;
}
autoit=std::find_if(databases.begin()、databases.end()、[&name](const auto&database){return name.compare(database.name)==0;});
if(it!=databases.end())
{
cout name您也可以使用,这将使您的代码看起来像:
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;
}
autoit=std::find_if(databases.begin()、databases.end()、[&name](const auto&database){return name.compare(database.name)==0;});
if(it!=databases.end())
{
CuxNo.P>一个更“现代C++”的方法是利用C++算法(例如),也许是说明符。
例如(假设数据库是std::vector或某种STL容器):
autoit=std::find_if(database.begin()、database.end()、[&name](const auto&item){返回name.compare(item.name)==0;};
if(it!=database.end())
{
CuxNo.P>一个更“现代C++”的方法是利用C++算法(例如),也许是说明符。
例如(假设数据库是std::vector或某种STL容器):
autoit=std::find_if(database.begin()、database.end()、[&name](const auto&item){返回name.compare(item.name)==0;};
if(it!=database.end())
{
cout name Just removecout Help us Help you,请提供一个“最小的、完整的、可验证的示例”我知道这很混乱,对此我很抱歉,但我的代码不是英文的,所以我不得不更改变量的名称和所有内容来发布这篇文章,所以很难重写所有内容:/基本上有办法显示这两种情况吗“找到产品”或“数据库中不存在产品!”。因此,当它找到输入的名称时,也不会输出未找到的其他项目。只需删除无法帮助我们帮助您,请提供“最小、完整且可验证的示例”“我知道这很混乱,对此我很抱歉,但我的代码不是英文的,所以我不得不更改变量的名称和所有内容来发布这篇文章,所以很难重写所有内容:/基本上有没有办法显示“找到的产品”或“数据库中不存在的产品!”所以,当它输入了名字,它也不会输出其他物品没有找到。非常感谢,先生!我已经打碎了我的头这么长时间,而不是立即去这里问。再次,非常感谢!干杯!很高兴我帮助了你,但是因为你的是一个非常初级的问题,我建议你多学习C++专业。语法语言(为初学者准备的大量书籍和在线教程/课程)在StackOverflow上提出此类问题之前。我理解,我也打算这样做!很抱歉给您带来不便。非常感谢您的好意,先生!我已经撞碎了我的头很长时间了,没有立即到这里来提问。再次,非常感谢您!干杯!很高兴我帮助了您,但因为您的问题确实是一个非常初级的问题,我建议在您在STACKOPPORT上问到这样的问题之前,请您多学习C++编程语言(大量的书和在线教程/课程)。我理解并且我打算。抱歉给您带来不便。