简体   繁体   English

我将随机生成的数字创建到数组中的程序不断崩溃

[英]My program to create randomly generated numbers into an array keeps crashing

So recently I've made a piece of code that randomly generates an array (or bundle) but it crashes when I try to run and I have no idea why.所以最近我做了一段代码,它随机生成一个数组(或包),但是当我尝试运行时它崩溃了,我不知道为什么。 I'm fairly new to coding and I'm unsure where the logical error is.我对编码相当陌生,我不确定逻辑错误在哪里。

So the program basically is meant to make a multi dimensional array that randomly generates "orders" for PC's.所以该程序基本上是为了制作一个多维数组,为 PC 随机生成“订单”。 I attempted to make the bundle that creates from 150-250 orders but if that isn't possible (or too complicated) I can make it just 250. The order is (orderId (just goes from 1 to however many orders there are), PCId (a random number 0-15), orderQuantity(random number from 1-20), dueDate (random number from 0-4), profit (not yet configured, just ignore for now)).我试图制作从 150-250 个订单创建的包,但如果这不可能(或太复杂),我可以只制作 250 个。订单是(orderId(从 1 到有多少订单), PCId(0-15 的随机数)、orderQuantity(1-20 的随机数)、dueDate(0-4 的随机数)、利润(尚未配置,暂时忽略))。 And in the array it is this order在数组中是这个顺序

{orderId, PCId, orderQuantity, dueDate, profit}

I would like to understand why it doesn't show any errors in the IDE but crashes when running!我想了解为什么它在 IDE 中不显示任何错误,但在运行时崩溃!

#include <iostream>
#include <cstdlib>
#include <ctime>

class BundleGenerator
{
public:
    BundleGenerator();
    void generateBundle();

protected:
    int numberOfOrders;
    int bundleArray[][5];
    int orderId;
    int PCId;
    int quantity;
    int dueDay;
    int profit;
};

int main()
{
    srand(time(NULL));

    BundleGenerator bg;
    bg.generateBundle();
    return 0;
}

BundleGenerator::BundleGenerator()
{
    int randNum;
    randNum = rand()%(250-150 + 1) + 150;
    numberOfOrders = randNum;
}
void BundleGenerator::generateBundle()
{
    orderId = 1;

    for (int i=0; i<numberOfOrders; i++)
    {
        bundleArray[i][0] = orderId;
        orderId++;

        int PCIdRnd = rand()%15;
        PCId = PCIdRnd;
        bundleArray[i][1] = PCId;

        int quantityRnd = rand()%19;
        quantity = quantityRnd;
        bundleArray[i][2] = quantity;

        int dueDayRnd = rand()%4;
        dueDay = dueDayRnd;
        bundleArray[i][3] = dueDay;

        bundleArray[i][4] = 0;
    }
}

Any advice on the program would be awesome!任何关于该计划的建议都会很棒! Thanks :)谢谢 :)

问题将出在“int bundleArray[][5];”上,看来您没有为数组提供任何内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM