我如何分离以下语句,以便我可以显示"123"
搜索的信息而不显示"topic not supported"
,因为它不是第一次搜索的"decimals"
?
string sentence;
string search;
string sentence2;
string search2;
size_t position2;
cin >> choosetopic;
switch (choosetopic)
{
case 1:
cout << "Please enter the name of the topic you need help with.";
cin >> sentence;
system("cls");
cout << "Topic: " << sentence << endl;
cout << "\n";
size_t pos;
search = "Decimals";
search = "decimals";
search = "decimal";
search = "Decimal";
pos = sentence.find(search);
if (pos != std::string::npos)
cout << "blah blah" << std::endl;
else
cout << "Topic not yet supported, please try another. View 'Help' on the main menu to see supported topics. " << endl;
cin >> sentence;
cout << "Topic: " << sentence << endl;
cout << "\n";
search = "123";
pos = sentence.find(search);
if (pos != std::string::npos)
cout << "12313" << std::endl;
else
cout << "Topic not yet supported, please try another. View 'Help' on the main menu to see supported topics. " << endl;
答案 0 :(得分:0)
尝试更像这样的东西:
int containsTopic(const std::string &s, const char **topics, int numTopics)
{
for(int i = 0; i < numTopics; ++i)
{
if (s.find(topics[i]) != std::string::npos)
return i;
}
return -1;
}
std::string sentence;
int choosetopic;
std::cin >> choosetopic;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (choosetopic)
{
case 1:
{
std::cout << "Please enter the name of the topic you need help with.";
std::cin >> sentence;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// better: std::getline(std::cin, sentence);
system("cls");
std::cout << "Topic: " << sentence << std::endl;
std::cout << "\n";
char* search[] = {"Decimal", "decimal", "123"};
int idx = containsTopic(sentence, search, 3);
switch (idx)
{
case 0:
case 1:
std::cout << "blah blah" << std::endl;
break;
case 2:
std::cout << "12313" << std::endl;
break;
default:
std::cout << "Topic not yet supported, please try another. View 'Help' on the main menu to see supported topics. " << std::endl;
break;
}
//...
break;
}
//...
}