Killer_B
Killer_B

Reputation: 63

stack around variable is corrupted. C visual studio

I am making a program to convert weight and height from metric to US and vice versa. I did the height part successfully, but the part with the weight is giving me a runtime error that stack around variable was corrupted.

I know that happens with arrays because pretty much thats all I get when I google the issue, but this is happening with a regular integer variable in 2 different functions.

This is the function that calls other functions to convert weight, one is for input, one is to convert and one is for output:

void weight_to_metric(void){

    int kilograms, pounds;

    double grams, ounces; 

    int * pkilograms= &kilograms, *ppounds=&pounds;

    double * pgrams=&grams, *pounces=&ounces;


    input_us_weight(ppounds, pounces);

    weight_us_to_metric(ppounds, pounces, pkilograms, pgrams);

    output_metric_weight(pkilograms, pgrams);

}

this is the function that inputs

void input_us_weight(int* feet, double * inches){
    printf("enter the number of pounds you want to convert: ");

    scanf(" %lf", feet, "\n");

    printf("enter the number of ounces you want to convert: ");

    scanf(" %lf", inches, "\n");

}

this is the function that converts

void weight_us_to_metric(int* pounds, double* ounces, int* kilograms, double * grams){

    double temp_kilograms;
    *kilograms = *pounds / 2.2046 + ((*ounces / 16) / 2.2046);

    temp_kilograms = *pounds / 2.2046 + ((*ounces / 16) / 2.2046);
    *ounces = ((temp_kilograms - *kilograms) *2.2046)*16;

    *grams = ((*ounces / 16.0)/2.2046) * 1000;
}

The output function doesn't even deal with the variable that corrupts. The variable that corrupts is pounds. The integer pounds declared in the initial variable.

How do i fix this?

Upvotes: 1

Views: 194

Answers (1)

Spikatrix
Spikatrix

Reputation: 20244

You are using wrong format specifiers in one of your scanfs. Use %d when scanning an int and %lf when scanning a double. Change

scanf(" %lf", feet, "\n");

to

scanf("%d", feet);

Also, remove the third argument("\n") that you pass to the scanfs. It makes no sense.

Upvotes: 3

Related Questions