I am trying to write a program to get student data (student id, subject results) from user input and align them based on their total marks. But when I try to input till the 5th student the stack smashing detected the error that occured. And I can't find a way to solve this problem...
It would be really helpful if anyone can give me some advice about it. Thanks in advance
My code: h file
#include <string>
#include <iostream>
using namespace std;
class student
{
private:
char id[100];
int eng, math, jan, total;
public:
void getInput(void);//ユーザーから学生のデータを入力してもらう
void putInput(void);//最後の結果を表示する
friend void sort(student std[]);//総得点の高い順に各人のデータを並べ替える
friend void avg(student std[]);//各科目と総得点の平均値を計算する
};
cpp file
#include <string>
#include <iostream>
#include "report1.h"
using namespace std;
#define MAX 10
void student::getInput(void)//ユーザーから学生のデータを入力してもらう
{
cout << "IDを入力してください:";
cin >> id;
cout << "英語の点数:";
cin >> eng;
cout << "数学の点数:";
cin >> math;
cout << "国語の点数:";
cin >> jan;
total = eng + math + jan;
}
void student::putInput(void)//最後の結果を表示する
{
cout << id << "\t" << eng << "\t" << math << "\t" << jan << "\t" << total << endl;
}
void sort(student std[])//総得点の高い順に各人のデータを並べ替える
{
int i, j;
for ( i = 0; i < MAX-1; i++)
{
for ( j = i+1; j < MAX; j++)
{
if (std[i].total < std[j].total)//一つ後ろのデータが確認中のデータより大きいなら、並べ替える
{
student temp;
temp = std[i];
std[i] = std[j];
std[j] = temp;
}
}
}
}
void avg(student std[])//各科目と総得点の平均値を計算する
{
int i;
int eng_total, math_total, jan_total, total_total;
double eng_avg, math_avg, jan_avg, total_avg;
eng_total = 0;
math_total = 0;
jan_total = 0;
total_total = 0;
for ( i = 0; i < MAX; i++)
{
eng_total += std[i].eng;
math_total += std[i].math;
jan_total += std[i].jan;
total_total += std[i].total;
}
eng_avg = (double)eng_total/MAX;
math_avg = (double)math_total/MAX;
jan_avg = (double)jan_total/MAX;
total_avg = (double)total_total/MAX;
cout << "Avg\t" << eng_avg << "\t" << math_avg << "\t" << jan_avg << "\t" << total_avg << endl;
}
int main()
{
student std[MAX-1];
int i, j;
for ( i = 0; i < MAX; i++)
{
cout << i+1 << "番目の学生データを入力してください。" << endl;
std[i].getInput();
}
sort(std);
cout << endl;
cout << "ID_No" << "\tEng" << "\tMath" << "\tJan" << "\tTotal" << endl;
for ( i = 0; i < MAX; i++)
{
std[i].putInput();
}
avg(std);
return 0;
}