gpt4 book ai didi

c++ - C++ 中的段错误(核心转储)

转载 作者:太空宇宙 更新时间:2023-11-04 03:47:58 25 4
gpt4 key购买 nike

我在编译 C++ 程序时遇到了这个恼人的错误。我查了一下,没有找到任何答案。这很奇怪,因为我的代码非常基本,根本不应该引起任何问题,但它确实引起了..我正在使用 Ubuntu 和 CODEBLOCKS IDE 来完成这项工作。完整代码如下:

#include <iostream>
#include <iomanip>
using namespace std;

struct database
{
string number;
string spec_number;
char group;
float grade;
string name;
string family_name;
};

void FillDatabase(database student[], short N,short &i);
void SplitDatabase(database student[], short N,short i);




int main()
{
short number_of_students,iteration=0;

//ПРОВЕРКА:
cout << "Za kolko studenta shte vyvejdate? ";
cin >> number_of_students;
cin.ignore();

database student[number_of_students];

FillDatabase(student,number_of_students,iteration);



//МЕНЮ ЗА ИЗБОР
SplitDatabase(student, number_of_students,iteration);


return 0;
}

//--------------------------------------------------------------------------------------------------------------------------------------------

void FillDatabase(database student[], short N,short &i)
{
for(i=0;i < N;i++)
{
//ДОПЪЛНИТЕЛНИ ПРОВЕРКИ ЗА ВАЛИДНОСТ:
cout << endl << endl << "VIE VYVEJDATE ZA STUDENT NOMER " << i+1 << "." << endl;
cout << endl << "Vyvedete fakulteten nomer na studenta...";
cin >> student[i].number;
cout << endl << "Vyvedete kod na specialnostta 52(E) ili 61(AIUT)...";
cin >> student[i].spec_number;
cout << endl << "Vyvedete grupa na studenta...";
cin >> student[i].group;
cout << endl << "Vyvedete sreden uspeh ot semestyra...";
cin >> student[i].grade;
cout << endl << "Vyvedete ime na studenta...";
cin >> student[i].name;
cout << endl << "Vyvedete familiya na studenta...";
cin >> student[i].family_name;
cin.ignore();
}

}

void SplitDatabase(database student[], short N,short i)
{
short count_of_E(0);
for(i=0;i < N;i++)if(student[i].spec_number=="52")count_of_E++;

database E_student[count_of_E];
database AIUT_student[N-count_of_E];

for(i=0;i < N;i++)
{
if(student[i].spec_number=="52")
{
E_student[i].number = student[i].number;
E_student[i].spec_number = student[i].spec_number;
E_student[i].group = student[i].group;
E_student[i].grade = student[i].grade;
E_student[i].name = student[i].name;
E_student[i].family_name = student[i].family_name;
}
else
{
AIUT_student[i].number = student[i].number;
AIUT_student[i].spec_number = student[i].spec_number;
AIUT_student[i].group = student[i].group;
AIUT_student[i].grade = student[i].grade;
AIUT_student[i].name = student[i].name;
AIUT_student[i].family_name = student[i].family_name;
}

}

cout << endl << endl << endl << left << "E" << setw(50) << right << "AIUT" << endl << endl;

for(i=0;i < N;i++)
{
cout << left << E_student[i].name << endl;
cout << left << E_student[i].family_name << endl;
cout << left << E_student[i].number << endl;
cout << left << E_student[i].group << endl;
cout << left << E_student[i].grade << endl << endl;

cout << setw(50) << right << AIUT_student[i].name << endl;
cout << setw(50) << right << AIUT_student[i].family_name << endl;
cout << setw(50) << right << AIUT_student[i].number << endl;
cout << setw(50) << right << AIUT_student[i].group << endl;
cout << setw(50) << right << AIUT_student[i].grade << endl;
}

}

/*
string number;
string spec_number;
char group;
float grade;
string name;
string family_name;



61462166 52 2 5.50
*/

我希望有人能帮我一把,我很沮丧......

最佳答案

问题(当运行时,而不是编译时)如下所示:

E_student[i].number = student[i].number;
E_student[i].spec_number = student[i].spec_number;

//...

AIUT_student[i].number = student[i].number;
AIUT_student[i].spec_number = student[i].spec_number;

您正在从 0..N-1 循环遍历 student - 但是 E_studentAIUT_student 小于 N。如果 N == 4,且 E_studentAIUT_student 的大小均为 2,则您在循环中最后一次分配给 AIUT_student[3]E_student[3] - 超出了相应数组的末尾。

您需要为每个数组保留单独的索引:

int ei = 0, ai = 0;

for (i = 0; i < N; i++)
{
if (student[i].spec_number == "52")
{
E_student[ei].number = student[i].number;
E_student[ei].spec_number = student[i].spec_number;
E_student[ei].group = student[i].group;
E_student[ei].grade = student[i].grade;
E_student[ei].name = student[i].name;
E_student[ei].family_name = student[i].family_name;
++ei;
}
else
{
AIUT_student[ai].number = student[i].number;
AIUT_student[ai].spec_number = student[i].spec_number;
AIUT_student[ai].group = student[i].group;
AIUT_student[ai].grade = student[i].grade;
AIUT_student[ai].name = student[i].name;
AIUT_student[ai].family_name = student[i].family_name;
++ai;
}
}

关于c++ - C++ 中的段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27361144/

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