编译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
*/
我希望有人借给我一个手,我很沮丧......
答案 0 :(得分:0)
问题(当正在运行,而不是正在编译时)是这样的行:
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;
您从student
循环0..N-1
- 但E_student
和AIUT_student
小于N
。如果N == 4
,E_student
和AIUT_student
的大小均为2
,那么您在整个循环中的最后一次分配给AIUT_student[3]
或{{ 1}} - 超过各个数组的末尾。
您需要为每个数组保留单独的索引:
E_student[3]
答案 1 :(得分:0)
你的问题是你使用整个学生数组的索引来处理部分数组AIUT_student和E_student。这将导致最新的访问冲突N / 2 + 1