每当我调用此函数时,我的程序停止工作,我似乎无法弄明白为什么。这是我的代码片段:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
void FindReplace(char Memory[], string FindWord, string ReplaceWord, const int MAXNUMCHARS)
{
int i = 0;
int SizeWord = FindWord.length();
int SizeReplace = ReplaceWord.length();
for(i = 0; i <= MAXNUMCHARS; i++)
{
if(Memory[i] == FindWord[i] && Memory[i+1] == FindWord[i+1] && Memory[i+2] == FindWord[i+2])
{
Memory[i] = ReplaceWord[i];
Memory[i+1] = ReplaceWord[i+1];
Memory[i+2] = ReplaceWord[i+2];
}
}
}
并且代表了调用它的代码
if(option == 'F')
{
FindReplace(Memory, FindWord, ReplaceWord, MAXNUMCHARS);
OutputMemoryToFile(Memory , Out);//copies array to output file
cout << "String 'the' has been found and replaced with 'can'";
cout << endl;
}
请问任何想法? :/
答案 0 :(得分:2)
您可能希望在break;
声明中找到if
;
答案 1 :(得分:2)
据我所知,您的调用代码无法确保Memory
,FindWord
和ReplaceWord
长度至少为MAXNUMCHARS+3
个字符(虽然这是一个猜测,因为你没有发布调用代码)。
答案 2 :(得分:0)
你也可以走出界限 你检查我&lt; = MAXNUMCHARS,但是使用i + 2