C &引用;分段故障(堆芯倾倒)“;当我运行程序时

C &引用;分段故障(堆芯倾倒)“;当我运行程序时,c,core,principles,C,Core,Principles,(顺便说一句,对不起我的英语不好) 我正在复制这个代码的解决方案。我的代码与解决方案相同(代码没问题,做得很好),但当我运行程序时,会出现一条消息,上面写着分段错误(内核转储)。我不知道如何向您展示我的程序的捕获,但我的代码似乎还可以。 当我运行程序时,当它询问货物的体积时,它就结束了。然后出现以下消息:分段故障(堆芯转储) #include <stdio.h> #define NUM 5 #define MAX_WAGON_CAPACITY 0.85 #define MAX_WA

(顺便说一句,对不起我的英语不好) 我正在复制这个代码的解决方案。我的代码与解决方案相同(代码没问题,做得很好),但当我运行程序时,会出现一条消息,上面写着
分段错误(内核转储)
。我不知道如何向您展示我的程序的捕获,但我的代码似乎还可以。 当我运行程序时,当它询问货物的体积时,它就结束了。然后出现以下消息:
分段故障(堆芯转储)

#include <stdio.h>

#define NUM 5
#define MAX_WAGON_CAPACITY 0.85
#define MAX_WAGON_CAPACITY_ANIMALS 0.5
#define LIMIT1 500
#define LIMIT2 2500
#define FRAGILE 1.10
#define DANGEROUS 1.15
#define FIRST_PRICE 0.50
#define SECOND_PRICE 0.45
#define THIRD_PRICE 0.40

typedef enum { FOOD, CHEMICAL, ANIMALS, VEHICLES,
               ELECTRONICS, CONSTRUCTION, OTHERS } tGoodType;
typedef enum { FALSE, TRUE } boolean;

int main(int argc, char **argv) {
    int idGood;
    float volumeGood;
    tGoodType typeOfGood;
    boolean isFragile;
    boolean isDangerous;
    float train [NUM];
    int nWagons;
    float volumeTrain;
    float price;  
    float surchargeFragile;
    float surchargeDangerous;

    printf("Good identifier: \n");
    scanf("%d", &idGood);
    printf("\nInsert volume of Good\n");
    scanf("%f", volumeGood);
    printf("\nInsert Good type (0-FOOD, 1-CHEMICAL, 2-ANIMALS, 3-VEHICLES, 4-ELECTRONICS, 5-CONSTRUCTION, 6-OTHERS)\n");
    scanf("%u", &typeOfGood);
    printf("\nIs the Good fragile? (0-FALSE, 1-TRUE)\n");
    scanf("%u", &isFragile);
    printf("\nIs the Good dangerous) (0-FALSE, 1-TRUE\n");
    scanf("%u", &isDangerous);
    printf("\nThe maximum length of the train is>> ");
    scanf("%f", train[0]);
    printf("\nThe length of the locomotive is>> ");
    scanf("%f", train[1]);
    printf("\nThe length of each wagon is>> ");
    scanf("%f", train[2]);
    printf("\nThe space between each wagon is>> ");
    scanf("%f", train[3]);
    printf("\nThe volume of a wagon is>> ");
    scanf("%f", train[4]);

    nWagons = (int)((train[1] - train[2]) / (train[3] + train[4]));

    if (typeOfGood == 2)
        volumeTrain = nWagons * train[4] * MAX_WAGON_CAPACITY_ANIMALS;
    else
        volumeTrain = nWagons * train[4] * MAX_WAGON_CAPACITY;

    price = 0.0;
    surchargeFragile = 0.0;
    surchargeDangerous = 0.0;
    if (volumeTrain >= volumeGood) {
        if (volumeGood > 0 && volumeGood < LIMIT1) { 
            price = volumeGood * FIRST_PRICE;
        } else if (volumeGood >= LIMIT1 && volumeGood <= LIMIT2) {
            price = volumeGood * SECOND_PRICE;
        } else {
            price = volumeGood * THIRD_PRICE;
        }
    }
    if (isFragile == 1) {
        surchargeFragile = (price * FRAGILE) - price;
    }
    if (isDangerous == 1) {
        surchargeDangerous = (price * DANGEROUS) - price;
        price = price + surchargeFragile + surchargeDangerous;
    }   

    if (price > 0.0) {
        printf("The Good id is %d", &idGood);
        printf("The number of wagons is %d", &nWagons);
        printf("The price for the good is %f", &price);
    } else {
        printf("The good does not fit the train");
    }

    return 0;
}
#包括
#定义NUM 5
#定义最大货车容量0.85
#定义最大载客量为0.5
#定义限制1500
#定义LIMIT2 2500
#定义1.10
#定义危险1.15
#定义首个_价格0.50
#定义二次价格0.45
#定义第三方价格0.40
typedef枚举{食品、化学品、动物、车辆、,
电子、建筑、其他}tGoodType;
typedef枚举{FALSE,TRUE}布尔值;
int main(int argc,字符**argv){
国际货物;
浮子体积;
tGoodType-typeOfGood;
布尔值是脆弱的;
布尔是危险的;
浮动列车[NUM];
尼瓦贡国际酒店;
浮动容积仪;
浮动价格;
浮动附加费;
浮动附加费危险;
printf(“良好标识符:\n”);
scanf(“%d”和&idGood);
printf(“\n插入货物卷\n”);
scanf(“%f”,volumeGood);
printf(“\n插入良好类型(0-食品、1-化学品、2-动物、3-车辆、4-电子、5-建筑、6-其他)\n”);
scanf(“%u”和“货物类型”);
printf(“\n是好的易碎品?(0-FALSE,1-TRUE)\n”);
scanf(“%u”、&isfragible);
printf(“\n是危险品)(0-假,1-真\n”);
scanf(“%u”和&isDangerous);
printf(“\n列车最大长度>>”;
scanf(“%f”,列[0]);
printf(“\n机车长度>>”;
scanf(“%f”,列[1]);
printf(“\n每辆货车的长度为>>”;
scanf(“%f”,第[2]列);
printf(“\n每辆货车之间的间距为>>”;
scanf(“%f”,第[3]列);
printf(“\n货车的体积为>>”;
scanf(“%f”,第[4]列);
nWagons=(int)(第[1]列-第[2]列/(第[3]列+第[4]列);
如果(货物类型==2)
volumeTrain=nWagons*列车[4]*最大载客量\动物;
其他的
volumeTrain=nWagons*列车[4]*最大货车容量;
价格=0.0;
附加费=0.0;
附加费=0.0;
如果(volumeTrain>=VolumeGod){
如果(volumeGood>0&&volumeGood=LIMIT1&&VolumeGod 0.0){
printf(“货物id为%d”,&idGood);
printf(“货车数量为%d”,&nWagons);
printf(“货物价格为%f”,&price);
}否则{
printf(“货物与列车不匹配”);
}
返回0;
}

您的scanf中有几个错误,产生了分段错误,它们由编译器指示:

因为VolumeGod的未定义值用作scanf尝试写入的地址

你可能想要

scanf("%f", &volumeGood);
还有所有这些:

因为序列中的条目使用起来就像它们包含地址一样,所以您需要

scanf("%f", &train[0]);
scanf("%f", &train[1]);
scanf("%f", &train[2]);
scanf("%f", &train[3]);
scanf("%f", &train[4]);
当您使用scanf和等效工具时,您必须给出保存值的地址


也在

c.c:84:29: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("The Good id is %d", &idGood);
                             ^
c.c:85:38: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("The number of wagons is %d", &nWagons);
                                      ^
c.c:86:40: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]
     printf("The price for the good is %f", &price);
那时候正好相反,你给出地址,而你必须给出值,必须是

     printf("The Good id is %d", idGood);
     printf("The number of wagons is %d", nWagons);
     printf("The price for the good is %f", price);

当你试图编译这个时,你的编译器告诉你什么?如果它没有给你警告,请启用编译器警告。如果它仍然没有给你警告,请找一个更好的编译器。修复警告,然后再次询问。@EOF
gcc-pedantic-Wextra-Wall c.c
除了修复后关于argv argv的警告之外,没有其他警告。嗯,这很尴尬。我是c完全错了。我可能应该去睡觉了。@chqrlie“这里”在哪里?无论如何,当我开始变得好斗时,我绝对需要摆脱堆栈溢出,而我完全错了(因为我显然不识字)。
scanf("%f", &train[0]);
scanf("%f", &train[1]);
scanf("%f", &train[2]);
scanf("%f", &train[3]);
scanf("%f", &train[4]);
c.c:84:29: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("The Good id is %d", &idGood);
                             ^
c.c:85:38: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("The number of wagons is %d", &nWagons);
                                      ^
c.c:86:40: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]
     printf("The price for the good is %f", &price);
     printf("The Good id is %d", idGood);
     printf("The number of wagons is %d", nWagons);
     printf("The price for the good is %f", price);