2017-04-04 1 views
0

"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; 
+2

search''에 그 과제는'여러 문자열을 검색하는 전화 find' 추가하거나 발생하지 않습니다. –

+0

어떻게 이런 일을 할 수 있습니까? – James941

+0

처음에는 "소수"를 찾으면 "십진수"를 찾으므로 후자는 중복됩니다. – chris

답변

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; 
    } 

    //... 
} 
관련 문제