2017-01-23 137 views
-1

我做一個比較簡單的程序來計算的長度×寬×高爲立方英寸。我應該得到一個像XXXXXXX.XX這樣的答案,但相反,編譯器給我0.000000編譯器不計算長度×寬×高爲立方英寸

我沒有其他錯誤。我的主要問題是如何獲得數字來計算?

#include <stdio.h> 

double length; 
double width; 
double height; 

// This is the volume in cubic inches. 
double VolumeCubicInch; 

// This is the volume in cubic feet. 
double VolumeCubicFeet; 

int main() { 
    // Ask the user to enter the length width and height of a box (in inches). 

    // First print asks user to enter the length of a box in inches. 
    printf("Please enter the length of a box in inches.\n"); 
    // The user reads in the length number of the box in inches. 
    scanf("%lf", &length); 

    // Second print asks user for the width of the box in inches. 
    printf("Now please enter the width of the box in inches.\n"); 
    // The user reads in the width number in inches. 
    scanf("%lf", &width); 

    // Then the third print asks user for the height of the box in inches. 
    printf("Please enter the height of box in inches.\n"); 
    // The user reads in the height of the box in inches. 
    scanf("%lf", &height); 

    // Calculate the volume of the box, in cubic inches and output the result. 
    // Using a newly created variable called VolumeCubicInch, it will allow the  calculation 
    // of the box's volume to be calculated in cubic inches. 
    // Length output given: 15.8 
    // Width output given: 23.34 
    // Height output given: 75.345 
    VolumeCubicInch = length * width * height; 
    // The resulted volume in cubic inches will be outputted using a print statement. 
    // Output should be: The volume is XXXXXXX.XX cubic inches. 
    printf("The volume is %lf cubic inches.\n", &VolumeCubicInch); 

    // Calculate the volume in cubic feet, and output the result. 
    // Using the variable VolumeCubicFeet will produce the volume in cubic feet. 
    // VolumeCubicFeet = ; 
    // The value or result of the volume in cubic feet will be outputted using  the variable VolumeCubicFeet. 
    // The output should be: The volume is XXXX.XX cubic feet. 
    // printf("The volume is %lf cubic feet.\n", &VolumeCubicFeet); 

    // Note that a box that is 12 x 12 x 12 inches is 1.0 cubic feet. 
    // *Be sure that your program gets that answer.* 

    system("PAUSE"); 
    return 0; 
} 
+5

這真的太糟糕了。不幸的是,沒有看到你的代碼,沒有任何人可以幫助你。祝你好運。 –

+0

請使用迄今爲止所做的工作。 –

+0

這不是編譯器給你0.000000。這是你的程序。 – DyZ

回答

4

您傳遞的結果,而不是它的價值地址:

printf("The volume is %lf cubic inches.\n", &VolumeCubicInch); 

而應該傳遞值,並指定2位小數:

printf("The volume is %.2f cubic inches.\n", VolumeCubicInch); 

注:

  • l修改有必要scanf轉換爲double格式而不是floatprintf總是促進floatdouble,所以%f格式用於兩者,並且如果指定的l被忽略。
  • 對於類型long double,您將在scanfprintf中使用%Lf轉換說明符。
  • 您可以通過在%f格式字符之間的小數點後傳遞精度字段來指定小數位數。
+0

@凱文:你是對的。 'l'對'scanf()'是必須的,但如果被'printf'忽略。 – chqrlie

+0

謝謝你解決了它! – John

+0

謝謝你提到我正要問關於浮球還是雙槳的說明。是否可以添加關於使用long float/long double的註釋? – John

1

兩件事情:

  1. scanf需要變量的地址是掃描,因爲它需要寫入這些變量。 printf不;它需要的參數的價值,因爲它只是讀/打印它們,因此該行:

    printf("The volume is %lf cubic inches.\n", &VolumeCubicInch); 
    

    應該僅僅是:

    printf("The volume is %lf cubic inches.\n", VolumeCubicInch); 
    
  2. 爲了打印出「XXXXXXX。 XX」(即保留兩位小數),作爲你在意見欄內註明您的輸出,你可以使用%.2格式長度修改,如:

    printf("The volume is %.2f cubic inches.\n", VolumeCubicInch); 
    
+0

謝謝你解決了 – John