数组数据无法正确计算输出

时间:2014-05-30 11:44:05

标签: c++ arrays terminal assignment-operator

所以我有一个重量计算器任务,我正在为Uni做,我的老师无法解决我的问题。我花了大约三天时间试图解决这个问题。我也手动执行了代码并追踪了这些步骤并且无法弄明白。

我没有收到任何错误,但计算不会产生合理的结果。

奇怪的是,它是一个非常小的程序,在多个设备上产生相同的结果,因此它不仅仅是我的PC或平板电脑。我认为问题可能在于首先存储重量数据,而不是计算本身。

我会尽量不要输入太多代码,但同时我希望你们了解我是如何构建程序的。我相信没有任何东西正确存储到result.weight

我对C ++很新,我已经过渡到Pascal了。如果我的代码中的逻辑有问题,请帮助我,因为它应该在我眼中工作。

part_data read_part()
{
    int option;
    part_data result;
    result.name = read_string("Please Enter a name: ");
    result.kind = read_part_kind();
    result.weight = read_float("Please Enter weight: ");
}

float weight_of_kind(part_array &data, partkind kind)
{
    int i = 0;
    float result = 0.0;

    for (i = 0; i< data.num; i++);
    {
        if (data.array[i].kind == kind)
        {
            result += data.array[i].weight;
        }
    }
    return result;
}

float total_weight(part_array &data)
{
    float result = 0.0;
    int i;
    for (i = 0; i < data.num; i++)
    {
        result += data.array[i].weight;
    }
    return result;
}

void calculate_weight(part_array &data)
{
    int i;
    printf("Total weight: %4.2f\n", total_weight(data));
    printf("Total CRITICAL: %4.2f\n", weight_of_kind(data, CRITICAL));
    printf("Total DESIRED: %4.2f\n", weight_of_kind(data, DESIRED));
    printf("Total OPTIONAL: %4.2f\n", weight_of_kind(data, OPTIONAL));
}

此外,以下是参考类型

enum partkind {
    CRITICAL, DESIRED, OPTIONAL
};

typedef struct
{
    my_string name;//array
    partkind kind;
    long weight;
} part_data;

typedef struct 
{
    part_data *array; // pointer to a part
    int num;          // number of parts
} part_array;

这是我在尝试生成多人计算时得到的输出。重量,按种类分类(严重,期望或可选)

enter image description here


更新: 以下是程序中可能有助于我们调试的其他相关函数。我很感激所有的帮助,而且你们已经避免了“挫败感”。我知道对某些人来说有多诱人

partkind read_part_kind()
{
        printf("1 - Critical\n");
        printf("2 - Desired\n");
        printf("3 - Optional\n");
    switch( read_integer_range("Please select a part kind: ", 1, 3) )
    {
        case 1: return CRITICAL;
        case 2: return DESIRED;
        default: return OPTIONAL;
    }
}

part_data read_part()
{
    int option;
    part_data result;

    result.name = read_string("Please Enter a name: ");
    result.kind = read_part_kind();
    result.weight = read_float("Please Enter weight: ");

    return result;
}

void add_part(part_array &data)
{
    data.num++;
    data.array = (part_data*) realloc(data.array, sizeof(part_data)*data.num);
    data.array[data.num - 1] = read_part();
}

我希望这段代码有助于更好地理解为什么输入没有被正确分配以及为什么总重量产生(看似)随机存储器。

3 个答案:

答案 0 :(得分:3)

你试图在函数 calculate_weight() total_weight()中使用指针 part_data * array 而不初始化它,这会导致它包含随机垃圾。在使用该数组之前,应该为它分配一个内存:

array = new part_data[n]

其中 n 是数组中元素的数量。在您的情况下,您可能希望 n 等于 data.num

此外,您声明 readPart()函数将返回 part_data ,但它不会返回任何内容。

答案 1 :(得分:2)

您的readPart()函数不会返回任何内容,但它应返回part_data。并尝试更改weight中的变量float而不是long,因为您的微积分会返回float

typedef struct {
    my_string name;//array
    partkind kind;
    float weight; // <<<<<<<
} part_data;

并在read_part()中返回part_data:

part_data read_part()
{
    int option;
    part_data result;
    result.name = read_string("Please Enter a name: ");
    result.kind = read_part_kind();
    result.weight = read_float("Please Enter weight: ");
    return result; // <<<<<<<
}

答案 2 :(得分:0)

使用指针代替引用。我会做的事情如下:

    part_data read_part()
    {
        int option;
        part_data result;
        result.name = read_string("Please Enter a name: ");
        result.kind = read_part_kind();
        result.weight = read_float("Please Enter weight: ");
        return result;
    }

    float weight_of_kind(part_array* data, partkind kind)
    {
        int i = 0;
        float result = 0.0;

        for (i = 0; i< data->num; i++);
        {
            if (data->array[i].kind == kind)
            {
                result += data->array[i].weight;
            }
        }
        return result;
    }

    float total_weight(part_array* data)
    {
        float result = 0.0;
        int i;
        for (i = 0; i < data->num; i++)
        {
            result += data->array[i].weight;
        }
        return result;
    }

    void calculate_weight(part_array* data)
    {
        int i;
        printf("Total weight: %4.2f\n", total_weight(data));
        printf("Total CRITICAL: %4.2f\n", weight_of_kind(data, CRITICAL));
        printf("Total DESIRED: %4.2f\n", weight_of_kind(data, DESIRED));
        printf("Total OPTIONAL: %4.2f\n", weight_of_kind(data, OPTIONAL));
    }

partkind read_part_kind()
{
        printf("1 - Critical\n");
        printf("2 - Desired\n");
        printf("3 - Optional\n");
    switch( read_integer_range("Please select a part kind: ", 1, 3) )
    {
        case 1: return CRITICAL;
        case 2: return DESIRED;
        default: return OPTIONAL;
    }
}

part_data read_part()
{
    int option;
    part_data result;

    result.name = read_string("Please Enter a name: ");
    result.kind = read_part_kind();
    result.weight = read_float("Please Enter weight: ");

    return result;
}

void add_part(part_array* data)
{
    data->num++;
    data->array = (part_data*) realloc(data->array, sizeof(part_data)*data->num);
    data->array[data.num - 1] = read_part();
}