段故障11

时间:2019-01-23 02:49:02

标签: c++ segmentation-fault cppcheck

我遇到了段故障11的错误。它将通过printall()命令运行,但似乎在printInvalidEmail命令中失败。

我还可以输入我创建的其他类。我有大约6个不同的类和这些类的头文件。我正在寻找从哪里开始解决此错误。

/*
 * roster.cpp
 *
 *  Created on: Jan 9, 2019
 *      Author: kaylasiemon
 */
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include "student.h"
#include "securityStudent.h"
#include "networkStudent.h"
#include "softwareStudent.h"
#include "degree.h"
#include "roster.h"

using namespace std;

roster::roster() {
}

roster::~roster() {
}

void roster::add(string studentID, string firstName, string lastName, string emailAddress, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, degree d){
    int courseDays[] = {daysInCourse1, daysInCourse2, daysInCourse3};
    if (d == degree::SECURITY)
        classRosterArray[iindex++] = new securityStudent(studentID, firstName, lastName, emailAddress, age, courseDays, d);

    if (d == degree::SOFTWARE)
        classRosterArray[iindex++] = new softwareStudent(studentID, firstName, lastName, emailAddress, age, courseDays, d);

    if (d == degree::NETWORK)
        classRosterArray[iindex++] = new networkStudent(studentID, firstName, lastName, emailAddress, age, courseDays, d);
}

void roster::remove(string studentID){
    bool removeStudent = false;
    for(int i = 0; i < 5; i++){
        if (classRosterArray[i] != NULL) {
            if(studentID == classRosterArray[i] ->getID()){
                classRosterArray[i] = nullptr;
                removeStudent = true;
            }
        }
    }
    if (removeStudent == false)
        cout << "Student ID invalid. No one to remove";
}

void roster::printAll(){
    cout << "Roster:" << endl;
    for (int i = 1; i < 6; i++)
        classRosterArray[i] -> print();
}

void roster::printByDegreeProgram(int degreeProgram){
    cout << "Displaying Roster by Degree Type:" << endl;
    degree deg1;
    if (degreeProgram == 0)
        deg1 = degree::SECURITY;
    if (degreeProgram == 1)
        deg1 = degree::NETWORK;
    if (degreeProgram == 2)
        deg1 = degree::SOFTWARE;
    for (int i = 1; i < 6; i++){
        if (deg1 == classRosterArray[i]->getDegreeProgram())
            classRosterArray[i] -> print();
    }
}

void roster::printDaysInCourse(string studentID){
    int average;
    for (int i = 1; i < 6; i++){
        if (classRosterArray[i] ->getID() == studentID){
            average = (classRosterArray[i]->getCourseDays()[0] + classRosterArray[i]->getCourseDays()[1] + classRosterArray[i]->getCourseDays()[2]) / 3;
            cout << average << endl;
        }
    }
}

void roster::printInvalidEmails(){
    cout << "invalid email addresses:" << endl;
    for (int i = 0; i < 5; i++){
        bool atSign = false;
        bool space = false;
        bool period = false;
        string email;
        email = classRosterArray[i] ->getemail();
        for (char &letter:email){
            if (letter == '@')
                atSign = true;
            if (letter == ' ')
                space = true;
            if (letter == '.')
                period = true;
        }
        if (atSign == false || space == false || period == false)
            cout << classRosterArray[i] ->getemail() << endl;
    }
}

int main(){
    const string studentData[] ={   "A1,John,Smith,John1989@gm ail.com,20,30,35,40,SECURITY",
                                    "A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK",
                                    "A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE",
                                    "A4,Erin,Black,Erin.black@comcast.net,22,50,58,40,SECURITY",
                                    "A5,Kayla,Siemon,ksiemon@wgu.edu,24,30,33,29,SOFTWARE"};

    roster classRoster;

    cout << "Scripting and Programming - Applications – C867" << endl << "Kayla Siemon" << endl <<"#000736769" << endl;
    degree degreepath;
    for (int i = 0; i < 5; i++){
        stringstream studentlist(studentData[i]);
        vector<string> results;

        while(studentlist.good()){
            string temp;
            getline(studentlist, temp, ',');
            results.push_back(temp);
        }
        if (results[8] == "SECURITY")
            degreepath = degree::SECURITY;
        if (results[8] == "NETWORK")
            degreepath = degree::NETWORK;
        if (results[8] == "SOFTWARE")
            degreepath = degree::SOFTWARE;
        classRoster.add(results[0], results[1], results[2], results[3], stoi(results[4]), stoi(results[5]), stoi(results[6]), stoi(results[7]), degreepath);
    }
    classRoster.printAll();
    classRoster.printInvalidEmails();
        classRoster.printDaysInCourse("A1");
    classRoster.printByDegreeProgram(SOFTWARE);
    classRoster.remove("A3");
    classRoster.remove("A3");

     return 0;
}

1 个答案:

答案 0 :(得分:3)

这是发展调试技能的好机会。您要做的第一件事是准确地 哪一行代码触发了崩溃。您可以使用调试器(通过逐步执行代码)来做到这一点,或者,如果愿意,也可以手动检测代码。例如,如果您认为崩溃是在printInvalidEmail()方法内发生的,则可以通过向该方法临时添加调试输出行,将其范围缩小到特定行,如下所示:

void roster::printInvalidEmails(){
    cout << "invalid email addresses:" << endl;
    for (int i = 0; i < 5; i++){
cout << "X1 " << i << endl;
        bool atSign = false;
        bool space = false;
        bool period = false;
cout << "X2 " << endl;
        string email;
cout << "X3 " << endl;
        email = classRosterArray[i] ->getemail();
cout << "X4 " << endl;
        for (char &letter:email){
cout << "X5 " << letter << endl;
            if (letter == '@')
                atSign = true;
            if (letter == ' ')
                space = true;
            if (letter == '.')
                period = true;
        }
cout << "X6" << endl;
        if (atSign == false || space == false || period == false)
        {
cout << "X7" << endl;
            cout << classRosterArray[i] ->getemail() << endl;
        }
cout << "X8" << endl;
    }
cout << "X9" << endl;
}

请注意,我插入了很多行,例如cout << "X1" << endl;,每行都打印一个唯一的字符串,因此我可以轻松区分它们。现在,当您运行程序时,将获得更多的输出,但是要看的主要是程序崩溃之前的最后一行文本输出-导致崩溃的代码很可能在崩溃之后输出该文本的行。

找到该行,它将(希望)使您知道该行可能崩溃的原因-例如,如果您看到的文本输出的最后一行是“ X3”,而您没有看到“ X4“之后打印,那么您可以确定行email = classRosterArray[i]->getemail();导致崩溃了……并且可能的原因是classRosterArray[i]是NULL或无效的指针(因此当您尝试取消对它的调用以调用getemail()时发生崩溃–然后,您需要跟踪在classRosterArray中设置条目的各个位置,以弄清为什么它们没有正确设置它。 (或者,也可能是getmail()方法内部有一个错误,并且程序在那里崩溃了–在这种情况下,您可以在该方法内部添加更多的调试输出以弄清位置和原因)< / p>

一旦弄清了导致崩溃的原因,并进行了修复,则可以返回并再次删除所有临时调试输出行。 (请注意,我故意不让所有临时cout调用缩进,这样一来,它们显然会从“真实”代码中突出显示,很容易在调试完成后返回并快速删除它们)