cin 在 12 次 for 循环迭代后使我的程序崩溃?

cin crashing my program after 12 for loop iterations?

我以前从未在这里发帖,但我真的卡住了,所以我想我会试一试。我已经研究这段代码一段时间了,目的是输入一些学生的分数,并将它们输出到包含平均值和总计的表格中。我得到了这样一个文件:

15
Albert Einstein 52 67 63
Steve Abrew 90 86 90 93
David Nagasake 100 85 93 89
Mike Black 81 87 81 85
Andrew Van Den 90 82 95 87
Joanne Dong Nguyen 84 80 95 91
Chris Walljasper 86 100 96 89
Fred Albert 70 68
Dennis Dudley 74 79 77 81
Leo Rice 95
Fred Flintstone 73 81 78 74
Frances Dupre 82 76 79
Dave Light 89 76 91 83
Hua Tran Du 91 81 87 94
Sarah Trapp 83 98

我的问题是,当我输入名字时,程序在 Fred Flinstone 之后崩溃,我知道这不是下一个名字 (Frances Dupre) 的格式问题,因为当我将他移到列表中时,它会读取它很好。

我找到了程序崩溃的地方,'cerr' 在读取过程的不同阶段输出,当它试图读取 Frances 的标记时崩溃。

a1main.cpp

#include <iostream>
#include "student.h"
using namespace std;

int main()
{
    int numStds;
    cin >> numStds;

    cerr << endl << "Num Stds: " << numStds << endl;

    Student std[numStds+1];

    for(int i = 0; i <= numStds; i++)
    {
        std[i].readData();
        std[i].printStudent();  
    }

    // delete [] std;

    return 0;
}

student.h

#include <iostream>
using namespace std;

class Student {

    private:

        char* name;
        int mark[4];
        int num;

    public:

        Student();
        ~Student();

        void readData();
        void printStudent();
        float getTotal();
        float getAverage();
};

student.cpp

#include <iostream>
#include <cstring>
#include <cctype>
#include "student.h"
using namespace std;

Student::Student()
{
        name = new char;
        mark[0] = 0;
        mark[1] = 0;
        mark[2] = 0;
        mark[3] = 0;
        num = 0;
}

Student::~Student()
{
    // Doesn't work?

    // delete name;
}

void Student::readData()
{   
    int l = 0;

    // Reading the Name
    cin >> name;        // Read in the first name
    l = strlen(name);   // get the strlength
    name[l] = ' ';      // Putting a space between the first and last name
    cin >> &name[l+1];  // Read in the last name

    cerr << endl << "I have read the name!" << endl;

    // Checking if there is a third name
    if(cin.peek() == ' ')
        cin >> ws;      // checking and navigating past the whitespace

    char next = cin.peek();
    if( isalpha(next) )     // Checking whether the next cin is a char
    {
        l = 0;
        l = strlen(name);
        name[l] = ' ';
        cin >> &name[l+1];
    }

    cerr << "I've checked for a third name!" << endl;

    // Reading in the marks
    for(int i = 0; i < 4; i++)
    {
        // Checks if the next cin is a newline
        if (cin.peek() == '\n')
            break;

        cin >> mark[i];
    }

    cerr << "I've read in the marks!" << endl;

    //cerr << endl << "I have read " << name << "'s marks!" << endl << endl;

    for(int m = 0; m < 4; m++)
    {   
        if(mark[m] != 0)
        {
            num++;
        }
    }

    cerr << "I've incremented num!" << endl << endl;
}

// Function for error checking
void Student::printStudent()
{
    cout << endl << "Student Name: " << name << endl;
    cout << "Mark 1: " << mark[0] << endl;
    cout << "Mark 2: " << mark[1] << endl;
    cout << "Mark 3: " << mark[2] << endl;
    cout << "Mark 4: " << mark[3] << endl;
    cout << "num marks: " << num << endl << endl; 
}

float Student::getTotal()
{}
float Student::getAverage()
{}

谁能看出我做错了什么?谢谢:)

您永远不会分配内存来存储学生姓名。

在您的 Student 构造函数中,添加以下代码:

name = new char[100]; // allows names up to 100 characters long

并取消注释析构函数中的这一行:

delete[] name;

您还可以通过测量名称长度并分配正确的大小来使代码更加复杂和健壮,或者按照下面的建议使用 std::string。