具有记录数组的 C++ 交换函数

C++ swap function with array of records

我刚开始学习C++,一直在做一些问题来磨练我的技能。目前我在交换记录数组的某些值时遇到问题。输入验证工作正常,但是当我尝试交换程序周围的值时,程序停止响应并崩溃。这是我创建它的方式:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int grandprixnum;

struct DriverData  {
    string driver;
    int car;
    string team;
    int grid;
    int points;
};

DriverData * grand = new DriverData[grandprixnum];

int input() 
{

    cout << "How many drivers where there? ";
    cin >> grandprixnum;
    cin.sync();

    DriverData * grand = new DriverData[grandprixnum];
    for (int i=0; i<grandprixnum; i++) 
    {
        cout << "Driver numbers: "<< i+1 << " \n";
        cout << "What is the drivers name? \n";
        getline (cin, grand[i].driver);
        cin.sync();

        cout << "What is the drivers car number? \n";
        cin >> grand[i].car;
        while (grand[i].car > 99 || grand[i].car < 1)
        {
                cout << "Please enter a value between 1 and 99! \n";
                cin >> grand[i].car;

        } 
        cin.sync();

        cout << "What team is the driver racing for? \n";
        getline (cin, grand[i].team);
        cin.sync();


        cout << "What grid are they in? \n";
        cin >> grand[i].grid;
        cin.sync();
        while (grand[i].grid < 0 || grand[i].grid > 22)
        {
                cout << "Please enter a grid number between 1 and 22! \n";
                cin >> grand[i].grid;

        } 
        cin.sync();

        cout << "What are their total points? \n";
        cin >> grand[i].points;
        cin.sync();
        while (grand[i].points > 25 || grand[i].points < 0)
        {
                cout << "Please enter the drivers points between 0 and 25! \n";
                cin >> grand[i].points;

        } 
    }
}

int sorting () 
//This part _______________________________
{   
    for(int a=1; a<=grandprixnum; a++)          
    {
        for(int b=0; b<=grandprixnum; b++)
        {
            if(grand[b].points < grand[b+1].points)
            {
                swap(grand[b].driver, grand[b+1].driver);
                swap(grand[b].car, grand[b+1].car);
                swap(grand[b].team, grand[b+1].team);
                swap(grand[b].grid, grand[b+1].grid);
                swap(grand[b].points, grand[b+1].points);
            }
        }
    }               
}
//To here_________________________________

int showtable ()
{
    cout << "Driver Car     Team    Grid    Points \n";
    for(int c=0; c<grandprixnum; c++)
    {
        cout <<  grand[c].driver << grand[c].car << grand[c].team <<    grand[c].grid <<    grand[c].points << "\n";
    }
}

int main() 
{
    input ();
    sorting ();
    showtable (); 
}

我环顾四周,找不到示例或与我有相同问题的人。如果有人能告诉我它有什么问题。提前谢谢你。

编辑:我之前测试过交换,它确实有效,但它似乎与记录数组有关。

您越界访问了您的数组。您的数组的长度为 grandprixnum,因此您可以访问从 0grandprixnum-1

的元素
for(int a=1; a < grandprixnum; a++)    
           //  ^      
{
    for(int b=0; b < grandprixnum-1; b++)
                // ^             ^^ -1 because of b+1
    {
        if(grand[b].points < grand[b+1].points)
        {
           ...
        }
    }
} 

如果函数中的 ter 与 return 无关,则不需要 return 类型。使用 void sorting()void input()void showtable()

您声明数组 grand 两次。在函数 input 中按时全局和第二次局部。将其声明为全局并在函数 input.

中分配它
DriverData * grand = NULL;

int input() 
{
    ...
    grand = new DriverData[grandprixnum];

从这一行中删除等号:

for(int b=0; b<=grandprixnum; b++)

所以会变成这样:

for(int b=0; b<grandprixnum; b++)

并替换这一行

DriverData * grand = new DriverData[grandprixnum];

grand = new DriverData[grandprixnum];  // will store in global variable