冒泡排序崩溃程序 C++

bubble sort crashing program c++

所以我一直在为 class 做一个项目,一切都进行得很顺利,直到我不得不按姓氏升序对信息进行排序。更详细地说,在我的程序中,我应该接受文件输入,将其应用到我认为合适的任何类型的变量中,通过将他们的答案与答案键进行比较来计算他们的成绩,然后按姓氏对条目进行排序。事不宜迟,这是我的代码! (温柔点)

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <algorithm>

const int TEST_SIZE = 10;

using namespace std;

struct StudentInfo
{
int id;
string fName;
string lName;
char testAnswers[10];
int totalPoints = 0;
int avg = 0;
char letterGrade;

};



void inputInfo(char[], StudentInfo[]);
void calcGrade(char[], StudentInfo[]);
void bubbleSort(StudentInfo[]);
void outputInfo(StudentInfo[]);

int main()
{
StudentInfo studentInfo[10];
string temp;
char answerKey[10];

inputInfo(answerKey, studentInfo);
calcGrade(answerKey, studentInfo);
bubbleSort(studentInfo);
outputInfo(studentInfo);

return 0;
}

void inputInfo(char answerKey[], StudentInfo studentInfo[])
{
cout << "Please enter the 10-question answer key: \n";

for(int i = 0; i < TEST_SIZE; i++)
    {
        cout << "Question " << i+1 << "\n";
        cin >> answerKey[i];
    }

    ifstream inFile("student.txt");

    for(int i = 0; i < TEST_SIZE; i++)
        {
            inFile >> studentInfo[i].id;
            inFile >> studentInfo[i].fName;
            inFile >> studentInfo[i].lName;

            for(int j = 0; j < TEST_SIZE; j++){
            inFile >> studentInfo[i].testAnswers[j];
            }
        }
}

void calcGrade(char answerKey[], StudentInfo studentInfo[])
{
for(int i = 0; i < TEST_SIZE; i++)
    {
        for(int j = 0; j < TEST_SIZE; j++)
            {
                if(studentInfo[i].testAnswers[j] == answerKey[j])
                    {
                        studentInfo[i].totalPoints += 5;
                    }
                    studentInfo[i].avg = studentInfo[i].totalPoints * 2;

                    if(studentInfo[i].avg >= 90)
                        {
                            studentInfo[i].letterGrade = 'A';
                        }
                    else if(studentInfo[i].avg >= 80)
                        {
                            studentInfo[i].letterGrade = 'B';
                        }
                    else if(studentInfo[i].avg >= 70)
                        {
                            studentInfo[i].letterGrade = 'C';
                        }
                    else if(studentInfo[i].avg >= 60)
                        {
                            studentInfo[i].letterGrade = 'D';
                        }
                    else
                        {
                            studentInfo[i].letterGrade = 'F';
                        }
            }
        }
}

void bubbleSort(StudentInfo studentInfo[])
{
StudentInfo temp;
int i;
int j;

for(i = 0; i < (TEST_SIZE-1); i++)
    {
        for(j = 0; j < TEST_SIZE; j++)
            {
                if(studentInfo[j].lName < studentInfo[j-1].lName)
                    {
                        temp = studentInfo[j];
                        studentInfo[j] = studentInfo[j-1];
                        studentInfo[j-1] = temp;
                    }
            }
    }
}

void outputInfo(StudentInfo studentInfo[])
{
cout << setprecision(1) << fixed;

cout << "Student ID\tStudent Name\tAnswers\tTotal Pts\tAverage\t   Letter Grade" << endl;

for(int i = 0; i < TEST_SIZE; i++)
    {
        cout << studentInfo[i].id << "\t";
        cout << studentInfo[i].lName << " ";
        cout << studentInfo[i].fName << "\t";

        for(int j = 0; j < TEST_SIZE; j++)
            {
                cout << studentInfo[i].testAnswers[j];
            }
        cout << "\t" << studentInfo[i].totalPoints << "\t";
        cout << studentInfo[i].avg << "\t";
        cout << studentInfo[i].letterGrade << "\n";
    }
}

我已经尽力了,但我的程序总是崩溃。假设在冒泡排序期间,因为没有该部分它工作正常。如果有人能告诉我我错在哪里,我将不胜感激。对于给您带来的任何不便,我们深表歉意。

这里发生了什么

temp = studentInfo[j];
studentInfo[j] = studentInfo[j-1];
studentInfo[j-1] = temp;

什么时候j==0?您越界访问。你最好使用 <algorithm> 中的 std::swap,比如

std::swap(studentInfo[j], studentInfo[j+1]);

确保您 运行 j 直到 TEST_SIZE - 1。或者编写 "manual" 交换,但 jj+1 交换。