gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:26:31 26 4
gpt4 key购买 nike

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

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”输出而崩溃的位置,并且在尝试读取弗朗西斯的标记时崩溃了。

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

学生.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。

关于c++ - cin 在 12 次 for 循环迭代后使我的程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29090817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com