0

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;
}
  • 2
    `student std[MAX-1];`, yet the array is consistently iterated `MAX` times. Consider defining `student std[MAX];` instead. If writing off the end of the array is a common cause of corrupting ("smashing") the stack. – user4581301 Jul 11 '21 at 16:12
  • 2
    Not directly related to the problem, but it's highly advisable _not_ to name anything `std`, as this has a specific meaning in C++ as being the standard namespace. Also I recommend reading ["Why is `using namespace std;` considered bad practice?"](https://stackoverflow.com/q/1452721/1678770) – Human-Compiler Jul 11 '21 at 16:34
  • Extending on the above, `using namespace std;` in a header can be particularly bad as it forces your design choice on anyone who needs to use the header. They add the header and suddenly totally unrelated code stops working. Most experienced programmers will figure out what happened pretty fast, but it's still not a nice thing to do to people. – user4581301 Jul 11 '21 at 16:42

0 Answers0