Julio
Julio

Reputation: 11

"Segmentation fault (core dumped)" when I run the program

(Sorry for my bad English btw) Well, I'm eve copying the solution of this code. My code is identical to the solution (the code it's ok, it's well done), but when I run the program, it appear a message that says Segmentation fault (core dumped). I don't know how to show you captures of my program, but my code seems alright. When I run the program it finishes when it asks for the volume of the Good. Then this message: Segmentation fault (core dumped) appears

#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;
}

Upvotes: 1

Views: 313

Answers (1)

bruno
bruno

Reputation: 32596

you have several error in your scanf, producing your segmentation fault, they are indicated by the compiler :

c.c:38:1: warning: ‘volumeGood’ is used uninitialized in this function [-Wuninitialized]
scanf("%f", volumeGood);
^~~~~~~~~~~~~~~~~~~~~~~

and

c.c:38:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", volumeGood);

because the undefined value of volumeGood is used as an address where scanf will try to write

you probably wanted

scanf("%f", &volumeGood);

and also all these ones :

c.c:46:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[0]);
        ^
c.c:48:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[1]);
        ^
c.c:50:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[2]);
       ^
c.c:52:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[3]);
        ^
c.c:54:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[4]);

because the entries in train are used like if they contains addresses, you wanted

scanf("%f", &train[0]);
scanf("%f", &train[1]);
scanf("%f", &train[2]);
scanf("%f", &train[3]);
scanf("%f", &train[4]);

when you use scanf and equivalent you have to give the addresses where the values will be saved


Also in

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);

that time it is the reverse, you give the address while you have to give the values, must be

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

Upvotes: 1

Related Questions