2016-06-07 8 views
0

이것은 지금까지 제 프로그램입니다. 그것은 컴파일되지만 마지막 부분에서 멈추고 충돌합니다. 나는 사용자의 문자열 입력을 반복하고 문자열에서 발견 된 잘못된 단어를 "****"로 대체하려고합니다. 내 오류는 find_Poop_inSentence에있을 가능성이 큽니다. "디버그 어설 션이 실패했습니다. 벡터 첨자 범위를 벗어"사용자 입력을 벡터에 저장된 값과 비교하십시오.

void find_Poop_inSentence(vector<string> & v1, vector<string> & v2, string sub); 

int main() 
{ 
cout << "Howdy partner, tell me some words you don't take kindly to.\n"; 
vector<string>bad_words; 
string word; 

while (cin >> word) 
{ 
    cin.ignore(); 
    bad_words.push_back(word); 
    if (word == "exit") 
     break; 

} 
cout << "Ok partner, got it!\n"; 
cout << "Now say something and I'll repeat it back to you. Don't worry, I'll bleep out the words that you don't like.\n"; 

word = ""; 
vector<string> random_sentence; 
while (cin >> word) 
{ 
    cin.ignore(); 
    random_sentence.push_back(word); 
    if (cin.get() == '\n') 
     break; 

} 

find_Poop_inSentence(bad_words, random_sentence, "****"); 

cout << "You said: "; 
for (unsigned int i = 0; i < random_sentence.size(); ++i) { 
    cout << ' ' << random_sentence[i]; 
} 
system("Pause"); 
return 0; 
} 

void find_Poop_inSentence(vector<string> & v1, vector<string> & v2, string sub) { 
int iterOne; 
int iterTwo = 0; 
int iteratorMax = v2.size(); 


for (iterOne = 0; iterOne < iteratorMax; iterTwo++) { 

    if (v1[iterOne] == v2[iterTwo]) { 
     v2[iterTwo] == sub; 
    } 
    if (iterTwo == iteratorMax) { 
     iterOne++; 
     iterTwo = 0; 
    } 

    } 
} 

답변

0

제 친구 인 이반 드라고에게 감사드립니다. 저는 이것을 해결할 수있었습니다.

void find_Poop_inSentence(vector<string> & v1, vector<string> & v2, string sub); 

int main() 
{ 
cout << "Howdy partner, tell me some words you don't take kindly to.\n"; 
vector<string>bad_words; 
string word; 

while (cin >> word) 
{ 
    //cin.ignore(); 
    bad_words.push_back(word); 
    if (word == "exit") 
     break; 

} 
cout << "Ok partner, got it!\n"; 
cout << "Now say something and I'll repeat it back to you. Don't worry, I'll bleep out the words that you don't like.\n"; 
cout << "Push enter twice when done.\n"; 

word = ""; 
vector<string> random_sentence; 
while (cin >> word) 
{ 
    //cin.ignore(); 
    random_sentence.push_back(word); 
    if (cin.get() == '\n') 
     break; 

} 

find_Poop_inSentence(bad_words, random_sentence, "****"); 

cout << "You said: "; 
for (unsigned int i = 0; i < random_sentence.size(); ++i) { 
    cout << ' ' << random_sentence[i]; 
} 
system("Pause"); 
return 0; 
} 

void find_Poop_inSentence(vector<string> & v1, vector<string> & v2, string sub) { 

for (unsigned int i = 0; i < v1.size(); i++) { 

    for (unsigned int j = 0; j < v2.size(); j++) { 
     if (v1[i] == v2[j]) { 
      v2[j] = sub; 
     } 

    } 
    } 

} 
0

당신은 물음표 단지 부분보다 할 일이 더있다. 교체 부품을 구현하더라도 코드는 작동하지 않습니다.

find(bad_words.begin(), bad_words.end(), say_back) != bad_words.end()) 

find()

처음 두 파라미터의 시작과 끝 반복자 값에 의해 주어진 순서를 검색한다. 이들은 bad_words입니다. find()은 세 번째 매개 변수가 제공 한 값의 첫 번째 발생을 검사하고 첫 번째 발견 된 값을 참조하는 반복기를 반환하거나 값을 찾을 수없는 경우 end()을 반환합니다.

bad_words에 "Fudge"가 포함되어 있고 say_back에 "Fudge"라고 입력하면 find()을 찾을 수 있습니다.

그러나 say_back에 "Definitely Fudge"를 입력하면 find()은 물론 찾을 수 없습니다. bad_words에 "Definitely Fudge"가 포함되어 있지 않으므로 정확하게 입력하십시오. find()은 정확한 일치를 검색합니다.

따라서 "say_back 문자열에있는 잘못된 단어를 바꾸려면이 방법이 효과가 없습니다.

bad_wordssay_back으로 대체하기 전에 올바른 알고리즘을 찾아야합니다. 각 개별 단어를 say_back에 넣은 다음 bad_words에있는 각 단어를 확인해야합니다.

정확하게 검색 알고리즘을 구현할 때까지 bad_words에서 찾은 내용을 바꾸는 방법은 말하기 전에 말 앞에 물건을 넣는 것입니다.

먼저 이것을 알아야합니다. 필요한 경우 언제든지 talk to your rubber duck 수 있습니다.

+0

fml. 편집하고 재 게시합니다. 죄송합니다. 많은 수면없이이 일을하고 있었고 뇌가 아팠습니다. 응답을 위해 ty – Ranfan

+0

는 편집하는 데 잠시 걸렸다. 그러나 나는 아직도 나빠져있다 – Ranfan

+0

나는 당신의 고무 오리와 말했어, 내가 제안했던 것에 따라? –

관련 문제