我一直收到相同的错误消息:Segment Fault(core dumped)

时间:2015-11-26 03:17:56

标签: c++ vector coredump

我对C ++和计算机相关的所有事情都很陌生,所以请轻松一点。我试图在我班级的作业中第一次使用矢量。我设法让我的代码进行编译,但在运行中途,代码读取segment fault (core dumped)。我不知道如何使用gdb精确定位确切的行但我知道它在循环期间出现在名称函数中以按字母顺序组织名称。我知道我的矢量大小有问题,但我不知道如何解决它。我还需要一个简单的解决方案,因为我不是高级课程,只能使用我迄今为止在课堂上学到的知识。

我也知道我的名字是多么愚蠢,事实上我已经把我的名字塞进了我的名字......

请帮助我,尽管我的代码性质令人困惑!

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

int choice(int);
void sortAndSend(vector<string> &names);

int main()
{
    ofstream outputFile;
    int numOfNames;
    int count = 0;
    numOfNames = choice(numOfNames);

    vector<string> names(numOfNames);
    sortAndSend(names);

    outputFile.close();
    return 0;
}

int choice(int a)
{
    cout << "Select a number 1-5 that reflects how many names" << endl
         << "you would like to enter." << endl;
    cin >> a;
    cout << "You have selected " << a << " names." << endl << endl;
    return a;
}

void sortAndSend(vector<string> &names)
{
    ofstream outputFile;
    string name;
    string a;

    cout << "You will be asked to enter " << names.size() << " names." << endl
         << "You may enter each individual's "
         << "last name first, followed by the individual's first name, "
         << "then middle name (if applicable)." << endl
         << "Do NOT include any commas." << endl << endl;
    cin.ignore();
    for (int count = 0; count < names.size(); count++)                           //Allows user to enter specified # of names
    {
        cout << "Enter a name now: ";
        getline(cin, name);
        names[count] = name;
        cout << endl << "You have entered " << names[count] << endl << endl;
    }

    for (int count = 0; count < names.size(); count++)
    {
        cout << names[count] << endl;
    }
    for (int count = 0; count < names.size(); count++)
                                      //Arranges names in alphabetical order
                                      //Error occurs here
    {
        while (names[count] > names[count + 1])
        {
            a = names[count + 1];
            names[count + 1] = names[count];
            names[count] = a;
        }
    }
    cout << "Your names are now alphebetized" << endl << endl;
    cout << "This is what is being copied to the file named"
         << " 'AlphabeticalOrderEC.txt': " << endl;

    for (int count = 0; count < names.size(); count++)         //Prints     names in alphabetical order
    {
        cout << names[count] << endl;
    }

    outputFile.open("AlphabeticalOrderEC");                    //Prints names into file
    for (int count = 0; count < names.size(); count++)
    {
        outputFile << names[count] << endl;
    }
}

1 个答案:

答案 0 :(得分:1)

您尝试访问不会向量矢量显示的地址。请参阅代码的这一部分:

for (int count = 0; count < names.size(); count++)
{
    while (names[count] > names[count + 1])
    {
        a = names[count + 1];
        names[count + 1] = names[count];
        names[count] = a;
    }
}

你不应该使用count + 1,只有当count小于names.size()-1时才必须这样做。您应该找到可以替换代码的这一部分的另一个逻辑。这就像试图访问只有10的数组的第11个元素。