2015-04-30 113 views
1

我正在製作一個程序來將體重和身高從公制轉換爲美國,反之亦然。我成功完成了高度部分,但具有重量的部分給我一個運行時錯誤,即堆棧周圍的變量已損壞。堆棧周圍的變量已損壞。 C visual studio

我知道發生在數組中,因爲當我谷歌問題時,我得到的就是這些,但這是在2個不同函數中發生的一個常規整數變量。

這是調用其它函數來轉換重量的功能,一個用於輸入,一個是轉換和一個用於輸出:

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

} 

這是輸入

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

} 
功能

這是轉換的功能

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

輸出功能不甚至處理腐敗的變量。腐敗的變量是磅。在初始變量中聲明的整數磅。

我該如何解決這個問題?

回答

3

您在您的scanf之一中使用錯誤的格式說明符。當掃描double時,掃描int%lf時使用%d。更改

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

scanf("%d", feet); 

此外,刪除您傳遞給scanf S中的第三個參數("\n")。這個不成立。

相關問題