C++ C++;程序使用指针和排序崩溃

C++ C++;程序使用指针和排序崩溃,c++,sorting,pointers,C++,Sorting,Pointers,我正在尝试编写一个程序,从用户那里获取学生的姓名和分数。然后对这些分数进行排序并取平均值。它使用指针来实现这一点。当我运行它时,它会进入询问名称和分数的部分。但它在收集数据后崩溃了 #include <iostream> #include <iomanip> #include <string> using namespace std; struct Data { string Name; double Grade; }; // Funct

我正在尝试编写一个程序,从用户那里获取学生的姓名和分数。然后对这些分数进行排序并取平均值。它使用指针来实现这一点。当我运行它时,它会进入询问名称和分数的部分。但它在收集数据后崩溃了

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct Data
{
    string Name;
    double Grade;
};

// Function prototypes
void getData(Data *, int);
void selectionSort(Data *, int);
double getAverage(Data *, int);
void displayData(Data *, int, double);

int main()
{
    Data *Test;         // To dynamically allocate an array
    double Average;     // To hold the average of the scores
    int Scores;         // To hold number of scores

    // Get number of scores
    cout << "How many scores do you have to average? ";
    cin  >> Scores;

    // Dynamically allocate an array larger enough
    // to hold the user-defined number of scores
    Test = new Data[Scores];    // Allocate memory

    getData(Test, Scores);

    selectionSort(Test, Scores);

    Average = getAverage(Test, Scores);

    displayData(Test, Scores, Average);

    delete [] Test;
    Test = 0;

    return 0;
}

void getData(Data *Test, int Scores)
{
    cout << "Enter the names and scores for each student.\n";
    for (int i = 0; i < Scores; i++)
    {
        cout << "Student #" << (i + 1) << endl;
        cout << "   Name: ";
        cin.ignore();
        getline(cin, (Test + i)->Name);
        do
        {
            cout << "   Score :";
            cin  >> (Test + i)->Grade;
            if ((Test + i)->Grade < 0)
                cout << "Scores must be greater than 0.\n" << "Re-enter ";

            cout << endl;
        } while ((Test + i)->Grade < 0);
    }
}

void selectionSort(Data *Test, int Scores)
{
    int startscan, minIndex;
    Data *minValue;

    for (startscan = 0; startscan < (Scores - 1); startscan++)
    {
        minIndex = startscan;
        *minValue = Test[startscan];
        for (int i = startscan + 1; i < Scores; i++)
        {
            if ((Test + i)->Grade < minValue->Grade)
            {
                *minValue = Test[i];
                minIndex = i;
            }
        }
        Test[minIndex] = Test[startscan];
        Test[startscan] = * minValue;
    }
}

double getAverage(Data *Test, int Scores)
{
    double Total;
    for (int i = 0; i < Scores; i++)
        Total += (Test + i)->Grade;

    return Total / Scores;
}

void displayData(Data *Test, int Scores, double Avg)
{
    cout << "    Test scores\n";
    cout << "Number of scores: " << Scores << endl;
    cout << "Scores in ascending-order:\n";
    for (int i = 0; i < Scores; i++)
        cout << (Test + i)->Name << ": " << (Test + i)->Grade << endl;

    cout << fixed << showpoint << setprecision(2);
    cout << "Average of scores: " << Avg << endl;
}
#包括
#包括
#包括
使用名称空间std;
结构数据
{
字符串名;
双级;
};
//功能原型
void getData(数据*,int);
无效选择排序(数据*,整数);
双平均值(数据*,整数);
无效显示数据(数据*,整数,双精度);
int main()
{
Data*Test;//动态分配数组
双倍平均;//保持分数的平均值
int Scores;//保存分数的数量
//获得分数
cout>得分;
//动态分配足够大的数组
//保存用户定义的分数数
测试=新数据[分数];//分配内存
获取数据(测试、分数);
选择排序(测试、分数);
平均值=平均值(测试,分数);
显示数据(测试、分数、平均值);
删除[]测试;
测试=0;
返回0;
}
void getData(数据*测试,整数分数)
{
cout分级;
返回总分/总分;
}
无效显示数据(数据*测试,整数分数,双平均值)
{

cout我可以看到两个错误:

错误1:

Data *minValue;

for (startscan = 0; startscan < (Scores - 1); startscan++)
{
    minIndex = startscan;
    *minValue = Test[startscan];  // <-- minValue is an uninitialized pointer

     //...
        if ((Test + i)->Grade < minValue->Grade)
        {
            *minValue = Test[i]; // <-- Same issue
            minIndex = i;
        }
double getAverage(Data *Test, int Scores)
{
    double Total;
    for (int i = 0; i < Scores; i++)
        Total += (Test + i)->Grade;  // <-- Total is uninitialized

对格式错误/缩进的代码进行了向下表决。“某些事件后崩溃”没有足够的描述性。也许你能识别这一行?顺便说一句,gdb工作得很好。但是它在收集数据后崩溃了——那么它崩溃在哪一行上呢?你知道如何确定,对吗?是的,有人进来,出于某种原因完全改变了我的帖子。我会修复它。你真的不需要太过火h
->
用法和指针。您的错误之一是由于您过度扩展自己,试图使代码看起来尽可能像指针。
double getAverage(Data *Test, int Scores)
{
    double Total;
    for (int i = 0; i < Scores; i++)
        Total += (Test + i)->Grade;  // <-- Total is uninitialized
double getAverage(Data *Test, int Scores)
{
    double Total = 0;