【问题标题】:Error, return value 3221225477错误,返回值 3221225477
【发布时间】: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”这个词,我也会收到同样的错误消息

【问题讨论】:

  • 这段代码无法编译;您需要提供一个最小的工作示例。例如,我们不知道splitSentcheckSent 做了什么,我们不知道words 来自哪里。此外,您需要解释您遇到的错误。
  • 是的,我刚刚修好了,你能再看一下吗?
  • child process exited with status 3221225477 说这将是一个分段错误 - 访问未分配的内存。我愿意打赌,因为您增加了i,然后再访问它。
  • 使用调试器,跟踪程序执行,观察局部变量,尤其是循环变量。

标签: c++


【解决方案1】:

正如 Obicere 在 cmets 中提到的,您可能会遇到分段错误,因为您手动递增 i,然后请求 words[i]。当变量i 已经达到应有的高度时,您可能只会在句子末尾出现“i”或“instructor”时才得到段错误。但后来你做了i++,所以i 比它应该的大一,words[i] 要求words 的一个元素超过它的结尾。这就是段错误发生的方式。

您应该更加小心您的if 语句以及它们之后会发生什么。顺便说一句,使用全局变量是个坏主意。在这种情况下,您不应在文件顶部定义words,而应通过引用传递它。此外,一个好的编辑器(emacs 或 vim)可以让您的代码更整洁。

我不完全确定你想要什么,所以我可能弄乱了流控制,但这段代码应该更接近你想要的。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void splitSent (string sent, vector<string>& words);

int main () {
  string sent;
  vector<string> words;

  cout << "Enter your sentence: " << endl;
  getline(cin, sent);
  splitSent(sent, words);

  string finalSent;
  for (unsigned int i = 0; i<words.size(); i++) {
    if (words[i] == "i") {
      finalSent += "I";
    } else if (words[i] == "instructor") {
      finalSent += "name of prof";
    } else {
      finalSent += words[i];
    }
    if (i<words.size()-1) { // Don't put a space before the period
      finalSent += " ";
    }
  }
  finalSent += ".";

  cout << "Final sentence is: " << finalSent << endl;

  return 0;
}

void splitSent (string sent, vector<string>& words) {
  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++;
  }
}

【讨论】:

    【解决方案2】:

    无需手动增加i。顺便说一下,for 循环就是这样做的。通过增加i,您超出了向量的大小并且显然访问了未定义的内存

    string finalSent;
        for (unsigned int i = 0; i < words.size(); i++)
        {
            if (words[i] == "i")
            {
                finalSent += "I ";
                continue;  
                //i++;
            }
    
            if (words[i] == "instructor")
            {
                finalSent += "name of prof ";
                continue;
                //i++;
            }
    
            finalSent += words[i];
            finalSent += " ";
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      相关资源
      最近更新 更多