이것은 내 첫 번째 게시물입니다. 저는 프로그래밍에 익숙하지 않고 C++을 사용하여 사용자가 텍스트 블록 (최대 500 자)을 제출하고 4 자 단어를 입력 할 수있게 해주는 프로그램을 만들 수 있다고 생각했습니다. 텍스트에서 그 단어를 골라낸 시간. 나는 X 코드를 사용하고 있으며 녹색 중단 점을 만들고 'for'루프 기능에서 프로그램을 일시 중지합니다. 내 코드는 아래와 같습니다 :Xcode에서 C++을 일시 중지
#include <iostream>
#include <string>
#include <math.h>
#define SPACE ' '(char)
using namespace std;
//Submit text (maximum 500 characters) and store in variable
string text;
string textQuery(string msgText) {
do {
cout << msgText << endl;
getline(cin, text); } while (text.size() > 500);
return text;
}
//Query word to search for and store as variable
string word;
string wordQuery(string msgWord) {
cout << msgWord << endl;
cin >> word;
return word;
}
//Using loop, run through the text to identify the word
int counter = 0;
bool debugCheck = false;
int searchWord() {
for (int i = 0; i < text.size(); i++) {
char ch_1 = text.at(i);
char ch_2 = text.at(i + 1);
char ch_3 = text.at(i + 2);
char ch_4 = text.at(i + 3);
cout << i;
if(ch_1 == word.at(0) &&
ch_2 == word.at(1) &&
ch_3 == word.at(2) &&
ch_4 == word.at(3))
{
counter++;
debugCheck = true;
}
}
return counter;
}
//cout the result
int main() {
string textUserSubmit = textQuery("Please submit text (max 500 characters): ");
string wordUserSubmit = wordQuery("Please select a word to search for: ");
int counterResponse = searchWord();
cout << debugCheck << endl;
cout << "The number of times is: " << counterResponse << endl;
return 0;
}
for 루프에서 오류가 발생합니다. 다른 단어, 여러 단어의 길이 및 텍스트에서 단어를 강조 표시하는 방법에 대한 내 프로그램 작동 방법에 대한 다른 조언이 도움이 될 것입니다. 누군가 내 문제를 도와 줄 수 있으면 정말 고맙겠습니다. 감사!
Welcome! 'while (text.size()> 500)'루프를 버퍼 변수를 사용하는 if 문으로 변경할 수 있습니다. while 루프는 사용자가 500 자에 도달하면 무한 루프됩니다. – ultifinitus
@ultifinitus 아니요, 그 프로그램의 일부분이 작동합니다. 루프는 원하는 크기 (500 자 이하)가 될 때까지 새로운 입력을 요구함으로써 입력이 '유효'한지 확인합니다. – bames53
@ bames53 이봐, 네가 맞아! 웬일인지 나는 그가 그의 루프와 함께 텍스트를 추가하고있다라고 생각했다, 단지 결석하는 것을 무시한다! – ultifinitus