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

标签 c++ arrays crash record swap

我刚刚开始学习 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)
        {
           ...
        }
    }
} 

如果函数中没有任何返回值,则不需要返回类型。使用void sorting()void input()void showtable()

您将数组声明为grand两次。函数input中的全局时间和本地时间。将其声明为全局并在函数 input 中分配它。

DriverData * grand = NULL;

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

关于C++ 带有记录数组的交换函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34511101/

相关文章:

c++ - 整数除法矩阵之和

php - 获取 URL 中正斜杠之间的变量

javascript - 在 JavaScript 中使用 {} 或 new Object() 创建一个空对象?

iphone - 转到上一个 ViewController 时应用程序崩溃

c++ - 由于未定义的行为或编译器错误导致 C++ 代码崩溃?

c++ - ESENT 二级索引已损坏,必须对数据库进行碎片整理

c++ - 开源 VoIP/SIP Windows C API

c++ - 如何在 win32 中更改窗口的背景图像?

arrays - 如果 Octave 中的条件未将所需值保存在数组中

android - Intent 相机使应用程序崩溃