【发布时间】:2016-07-24 19:31:30
【问题描述】:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector <string> words;
void splitSent (string sent);
int main ()
{
string sent;
cout << "Enter your sentence: " << endl;
getline (cin, sent);
splitSent (sent);
string finalSent;
for (unsigned int i = 0; i < words.size(); i++)
{
if (words[i] == "i")
{
finalSent += "I ";
i++;
}
if (words[i] == "instructor")
{
finalSent += "name of prof ";
i++;
}
finalSent += words[i];
finalSent += " ";
}
cout << "Final sentence is: " << finalSent << "." << endl;
return 0;
}
void splitSent (string sent)
{
int Pos = 0; // Position
string word;
while (Pos < sent.length())
{
while ((Pos < sent.length()) && (sent[Pos] != ' '))
{
word += sent[Pos];
Pos++;
if (sent[Pos] == '.')
{
break;
}
};
words.push_back(word);
word = "";
Pos++;
}
}
到目前为止,这是我的程序,我正在尝试将“i”替换为“I”,并将“instructor”替换为我的教授的姓名。但是,每次句子中有两个以上的“i”时,我都会不断收到错误消息,我不知道为什么。如果我的句子中有“instructor”这个词,我也会收到同样的错误消息
【问题讨论】:
-
这段代码无法编译;您需要提供一个最小的工作示例。例如,我们不知道
splitSent或checkSent做了什么,我们不知道words来自哪里。此外,您需要解释您遇到的错误。 -
是的,我刚刚修好了,你能再看一下吗?
-
child process exited with status 3221225477 说这将是一个分段错误 - 访问未分配的内存。我愿意打赌,因为您增加了
i,然后再访问它。 -
使用调试器,跟踪程序执行,观察局部变量,尤其是循环变量。
标签: c++