内存分配问题传递/返回Struct *数组

时间:2015-07-29 21:36:04

标签: c++ arrays pointers dynamic-memory-allocation

请帮我完成作业。我已经让这个程序在调试模式下运行得很好,但是一旦我使用发布模式,它就会以abort()崩溃。

我知道它可能与内存分配有关,但我不太了解指针。

要求是我必须使用*数组来动态分配内存。

  

“你的课程应该适用于任何数量的学生。当课程   开始时,它应该询问用户的学生人数   处理。然后它应该动态分配该大小的数组   (学生/分数结构的数组)。“

然后我需要,“调用函数输入学生姓名/分数对并将它们存储在数组中。”

那么我应该在函数的主要内部还是内部创建数组?如何在不搞乱内存分配的情况下返回/传递*数组?

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

using namespace std;

struct Student
{
    string name;    //student's name
    int score;      //student's score
};

//function prototypes
void inputNameScore(Student*, int&);
void sortArray(Student* , int);
double avgScore(Student* , int);
void displayTable(Student* , int, double);

int main()
{   
    Student* arrayOfStudentPtr;     //pointer of type student to receive returned array pointer
    int numberOfStudents;           //number of students to be entered by user
    double average;                 //total score average   

    cout << "Please enter the number of students: ";
    cin >> numberOfStudents;

    arrayOfStudentPtr = new Student[numberOfStudents];  //dynamic array of type Student assigned to pointer

    inputNameScore(arrayOfStudentPtr, numberOfStudents);
    sortArray(arrayOfStudentPtr, numberOfStudents);
    average = avgScore(arrayOfStudentPtr, numberOfStudents);

    displayTable(arrayOfStudentPtr, numberOfStudents, average);    

    return 0;
}

void inputNameScore(Student* arrayOfStudentPtr, int& numberOfStudents)
{   
    for (int i = 0; i < numberOfStudents; i++)
    {
        cout << endl << "Enter the name for student " << i + 1 << ": ";
        cin.ignore();
        getline(cin, arrayOfStudentPtr[i].name);
        cout << endl << "Enter the student's score: ";      
        cin >> arrayOfStudentPtr[i].score;
        while (arrayOfStudentPtr[i].score > 105 || arrayOfStudentPtr[i].score < 0)
        {
            cout << "Student's score can't be negative or greater than 105." << endl;
            cout << endl << "Enter the student's score: ";
            cin >> arrayOfStudentPtr[i].score;
        }
    }
}

void sortArray(Student* arrayOfStudentPtr, int numberOfStudents)
{
    Student Temp;   //holds a student struct object
    bool swap;      //swap is initialized to false at the start of each loop. If it is still false at end of loop we know there is nothing else to sort
    do
    {
        swap = false;
        for (int i = 0; i < numberOfStudents; i++)
        {
            if (arrayOfStudentPtr[i].score > arrayOfStudentPtr[i + 1].score)
            {
                Temp = arrayOfStudentPtr[i];
                arrayOfStudentPtr[i] = arrayOfStudentPtr[i + 1];
                arrayOfStudentPtr[i + 1] = Temp;
                swap = true;
            }
        }
    } while (swap);
}

double avgScore(Student* arrayOfStudentPtr, int numberOfStudents)
{
    int total;      //total of all grades
    double average; //average of all grades
    total = 0;

    for (int i = 0; i < numberOfStudents; i++)
    {
        total = arrayOfStudentPtr[i].score;
    }
    average = total / numberOfStudents;
    //average = static_cast<double>(total) / numberOfStudents;
    return average;
}

void displayTable(Student* arrayOfStudentPtr, int numberOfStudents, double average)
{   
    cout << endl << setw(31) << left << "Name" << setw(6) << right << "Score" << endl;
    cout << "-------------------------------------" << endl;
    for (int i = 0; i < numberOfStudents; i++)
    {
        cout << setw(31) << left << arrayOfStudentPtr[i].name << setw(6) << right << arrayOfStudentPtr[i].score << endl;
    }
    cout << "-------------------------------------" << endl;
    cout << setw(31) << left << "Average: " << setw(6) << right << endl;
}

1 个答案:

答案 0 :(得分:0)

以下代码可以使用。

void sortArray(Student* arrayOfStudentPtr, int numberOfStudents)
    {
        Student Temp;   //holds a student struct object
        bool swap;      //swap is initialized to false at the start of each loop. If it is still false at end of loop we know there is nothing else to sort
        do
        {
            swap = false;
            for (int i = 0; i < numberOfStudents-1; i++)
            {
                if (arrayOfStudentPtr[i].score > arrayOfStudentPtr[i + 1].score)
                {
                    Temp = arrayOfStudentPtr[i];
                    arrayOfStudentPtr[i] = arrayOfStudentPtr[i + 1];
                    arrayOfStudentPtr[i + 1] = Temp;
                    swap = true;
                }
            }
        } while (swap);
    }