이 코드를 입력하여 실행하려고하면 사용자가 옵션 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;
}
}
'while'과'main' 대괄호를 닫으면 컴파일되지 않습니다. 첫 번째 if 문이'choice'를 설정하고 있습니다. – Seth