2014-11-29 2 views
1

이 코드를 입력하여 실행하려고하면 사용자가 옵션 1을 선택하고 텍스트 내에서 검색 할 문자열과 문자열을 입력 할 때 작동하지 않습니다. 사용자에게 텍스트를 입력 할 필요없이 "Enter text"를 입력 한 다음 "enter string to search"를 출력합니다. 뭐가 잘못 되었 니?배열 내의 C++ 메뉴 및 문자열 검색

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <ctime> 
#include <iomanip> 
#include <algorithm> 

using namespace std; 

string s1, text; 

int rand(int*); 
int Array[100]; 
void sortArray(int[], int); 
void showArray(const int [], int); 

int main() 
{ 
    while (1) 

    // Menu to prompt user choice 
    { 
     char choice[1]; 

      cout << endl; 
      cout << endl; 
      cout << "--MENU--" << endl; 
      cout << "1. Pattern Matching" << endl; // search for string within text 
      cout << "2. Sorting Techniques" << endl; // generate and then sort 10 random numbers 
      cout << "Enter your choice: " << endl; 
      cout << endl; 
      cin >> choice; 
      cout << endl; 

     if (choice[0] == '1') // string search option 

     { 
      cout << "Enter text:" << endl; // accept text from user 
      getline (cin, s1); 

      cout << "Enter string to search:" << endl; // accept string to search from user 
      getline (cin, text); 

      int pos = s1.find(text); // finds position where the string is located within text 

      if (pos >= 0) 
      { 
       cout << "Found '" << text << "'" << " at position " << pos + 1 << "." << endl; 
      } 
      else 
      { 
       cout << "Did not find text." << endl; 
      } 

     } 

답변

2

cin >> choice는 사용자가 입력 한 검색을위한 현재의 입력 라인의 일부를 판독하기 때문이다. 첫 번째 getline() 호출은 사용자가 입력 한 선택 항목 바로 다음에 입력 행의 나머지 부분을 읽습니다. 선택 후에 나머지 입력 행을 무시해야합니다.

cin >> choice; 
cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

또한 numerical_limits 끌어 오기 위해 코드의 시작 부분에 #include <limits>을 추가해야합니다.

0

사용자 응답을 위해 일종의 char 배열을 정의하는 것처럼 보입니다. 선택 항목이 1 또는 2가 아닌 경우에는 0이 아닌 정수 유형을 예외로 사용하는 경향이 있습니다. 코드 행을 줄이는 출력 형식 지정을위한 몇 가지 바로 가기도 있습니다. 또한 표준 문자열 클래스를 포함하여 문자열을 허용하려고합니다. 다음과 같이 시도해보십시오.

#include <string> 
#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <ctime> 
#include <iomanip> 
#include <algorithm> 

using namespace std; 

string s1, text; 

int rand(int*); 
int Array[100]; 
void sortArray(int[], int); 
void showArray(const int [], int); 

int main() 
{ 
    while (1) 

    // Menu to prompt user choice  

{ 
    int choice; 

     cout << "\n--MENU--\n"l; 
     cout << "1. Pattern Matching\n"; // search for string within text 
     cout << "2. Sorting Techniques\n"; // generate and then sort 10 random numbers 
     cout << "Enter your choice:\n"; 
     cin >> choice+"\n"; 

if (choice == 1 && choice > 0 && choice != 0) // string search option 

    { 
     cout << "Enter text:" << endl; // accept text from user 
     getline (cin, s1); 

     cout << "Enter string to search:" << endl; // accept string to search from user 
     getline (cin, text); 

     int pos = s1.find(text); // finds position where the string is located within text 

     if (pos >= 0) 
     { 
      cout << "Found '" << text << "'" << " at position " << pos + 1 << ".\n"; 
     } 
     else 
     { 
      cout << "Did not find text.\n"; 
     } 

    }}} 
+0

'while'과'main' 대괄호를 닫으면 컴파일되지 않습니다. 첫 번째 if 문이'choice'를 설정하고 있습니다. – Seth