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

时间:2015-03-17 03:28:51

标签: c++ loops for-loop crash cin

我以前从未在这里张贴过但我真的被卡住了所以我想我会试一试。我一直在研究这段代码,目的是输入几个学生的标记,并将它们输出到具有平均值和总数的表格中。我得到了这样一个文件:

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()
{}

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

1 个答案:

答案 0 :(得分:0)

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

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

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

并在析构函数中取消注释该行:

delete[] name;

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