c++ - 读取字符串时发生核心转储?

标签 c++ string

我正在做一个介绍性的 C++ 作业,我必须读入一个字符串,然后计算字符串本身中字母的出现频率,并输出结果。我不允许修改我的任何函数头文件(如果可以的话,我现在可能不会在这里),而且我看到大部分较难的部分都被删除了。我遇到的唯一问题是我以前能够读取我的字符串,但我的程序无法正确计算第一个字符的出现次数。修复该问题后,我在读取字符串的函数中遇到了段错误。到目前为止我的代码是这样的:

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

//FUNCTION PROTOTYPES 

string getPhrase(const string & prompt);   
int charIndex(const vector<char> & list, char letter);
void addLetter(vector<char> & letters, vector<int> & freqs, char letter);

int main()
{
    // Define your local variables
const int COLUMNWIDTH = 2;  //will be used in later functions
vector<char> letters;  //list of letters in the string
vector<int> freqs;     //corresponding list of frequencies of letters
vector<char> list;     //not so sure whats up with this, but it
char letter;           //the current letter 
int index =-1;         //index location of letter, if == -1, then letter is not currently indexed and needs to be
string prompt;  //user input statement 

//Input string
const string phrase = getPhrase(prompt); 


    int i =0;
while (phrase[i] == ' ')//determine first term of phrase that isn't a space, then make that space the first term of list so next loop can run
  {
    i++;
  }
list[0] = phrase[i];

for (int i = 0; i<phrase.length(); i++)
  {
    index = charIndex(list,phrase[i]);
    if (phrase[i]!= ' ')
      {
    if (index == -1) 
      {
        letter = phrase[i];
        addLetter(letters, freqs, letter);
        list = letters;
        index = charIndex(list,letter);
      }
      }
  }


  return 0;
}



// FUNCTION DEFINITIONS GO HERE:
string getPhrase(const string &prompt)
{
  string phrase;
  std::cout<<"Enter phrase: ";    //**ERROR IS OCCURING HERE **
  std::getline(cin, phrase);      //**ERROR IS OCCURING HERE **

  return phrase;
}



 //determine the index location of the specific letter 
 int charIndex(const vector<char> &list, char letter)

 {
   int i = 0;
   int index = -1;
   while ( i <= list.size())
     {
       if (letter == list[i])
     {
       index = i;
       if (index != -1)
         {
           i = list.size() +1;
         }
     }
       i++;
     }
   return (index);
 }    

//addLetter adds the new letter to the list of letters, and the corresponding frequency list is changed 
void addLetter(vector<char> & letters, vector<int> & freqs, char letter)  
{
  letters.push_back(letter);
  freqs.push_back(1);

} 

有很多“const”我希望我能够删除,但我不能。我也已经通过并确定错误发生在我的“getLine”的“getPhrase”函数中,并且不知道是什么导致了它。

最佳答案

段错误发生在:

list[0] = phrase[i];

因为虽然您将 list 声明为 vector ,但实际上您还没有分配任何元素,所以 [0] 不存在。修复它的一种方法是这样做:

list.push_back(phrase[i]);

关于c++ - 读取字符串时发生核心转储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15938831/

相关文章:

c++ - 使用 PortAudio 和 sndfile 播放立体声 .wav 文件,输出模糊且变慢/变慢

c++ - 用于 C/C++ 的可移植且简单的 unicode 字符串库?

c++ - 更新 Windows 窗体 - Visual C++

java - 如何在java String中用 "\_"精确替换空格?

c - 将字符串传递给 C 中的结构体

c++ - 如何使用 VS2015 社区为 C++ 安装 ZeroMQ

c++ - 复制目录内容

python - 在 python 中,如何从任何列(字符串值)中存在特定字符串的数据框中获取行

c++ - 你如何连接两个字符串?

swift - 如何在swift中将 'Date'类型的值转换为 'String'