C++ 读取字符串时转储内核?

C++ 读取字符串时转储内核?,c++,string,C++,String,我正在做一个intro-C++作业,我必须读入一个字符串,然后计算字符串中字母的频率,并输出结果。我不允许修改我的任何函数头(如果可以的话,我现在可能不会在这里),而且我看到了大部分比较困难的部分。我遇到的唯一问题是,我以前能够读取字符串,但我的程序无法正确计算第一个字符的出现次数。在我解决了这个问题之后,我的函数在读取字符串时出现了一个分段错误。到目前为止,我的代码是: #include <iostream> #include <iomanip> #include &l

我正在做一个intro-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);

} 
#包括
#包括
#包括
#包括
使用名称空间std;
//功能原型
字符串getPhrase(常量字符串和提示符);
int charIndex(常量向量和列表,字符字母);
无效添加字母(矢量和字母、矢量和频率、字符字母);
int main()
{
//定义局部变量
const int COLUMNWIDTH=2;//将在以后的函数中使用
向量字母;//字符串中的字母列表
vector freqs;//字母频率的对应列表
vector list;//不太确定这是怎么回事,但它
char letter;//当前字母
int index=-1;//索引字母的位置,如果==-1,则字母当前没有索引,需要
字符串提示符;//用户输入语句
//输入字符串
const string phrase=getPhrase(提示);
int i=0;
while(短语[i]='')//确定短语的第一项不是空格,然后使该空格成为列表的第一项,以便下一个循环可以运行
{
i++;
}
列表[0]=短语[i];

对于(int i=0;i而言,SEG故障发生在:

list[0] = phrase[i];
因为尽管您将
列表
声明为向量,但实际上尚未分配任何元素,因此
[0]
不存在。解决此问题的一种方法是:

list.push_back(phrase[i]);

SEG故障发生在:

list[0] = phrase[i];
因为尽管您将
列表
声明为向量,但实际上尚未分配任何元素,因此
[0]
不存在。解决此问题的一种方法是:

list.push_back(phrase[i]);

@user2250690不客气,很高兴我能帮助你。请随意投票并标记为正确。@user2250690不客气,很高兴我能帮助你。请随意投票并标记为正确。事情通常是
const
有很好的理由的,学习适当地标记所有应该是
const
的内容符合你的最大利益。什么是
字符串提示符
的用途,并将其用作
字符串getPhrase(const string&prompt)中的参数
?您似乎没有用它做任何事情。事情通常是
const
,这是一个很好的理由,学习适当地标记所有应该是
const
的内容符合您的最大利益。
string prompt
的目的是什么,以及它在
string getPhrase(const string&prompt)中作为参数的用途是什么
?你似乎什么都没做。