2014-09-21 94 views
-1

我弄不明白为什么我的程序不会打印最终解决方案(totalDough)。输入是8,10,12然后40,100和200:打印解决方案(新的C)

const int DOUGH_PER_SQFT = 0.75; 
const int INCHES_PER_FEET = 12; 
#define M_PI 3.14159265358979323846 

int main(){ 

    // Declare and initialize variables 

    int smallRIn; 
    printf("What is the radius of your small pizza, in inches?\n"); 
    scanf("%d", &smallRIn); 
    int mediumRIn; 
    printf("What is the radius of your medium pizza, in inches?\n"); 
    scanf("%d", &mediumRIn); 
    int largeRIn; 
    printf("What is the radius of your large pizza, in inches?\n"); 
    scanf("%d", &largeRIn); 


    // Get number of pizzas sold 
    int smallSold; 
    printf("How many small pizzas do you expect to sell this week?\n"); 
    scanf("%d", &smallSold); 
    int mediumSold; 
    printf("How many medium pizzas do you expect to sell this week?\n"); 
    scanf("%d", &mediumSold); 
    int largeSold; 
    printf("How many large pizzas do you expect to sell this week?\n"); 
    scanf("%d", &largeSold); 

    // Convert radii to feet 

    double smallRFeet, mediumRFeet, largeRFeet; 
    smallRFeet = smallRIn/INCHES_PER_FEET; 
    mediumRFeet = mediumRIn/INCHES_PER_FEET; 
    largeRFeet = largeRIn/INCHES_PER_FEET; 

    // Calculate top surface areas of each type of pizza. 

    double areaSmall, areaMedium, areaLarge; 
    areaSmall = smallSold*M_PI*pow(smallRFeet,2); 
    areaMedium = mediumSold*M_PI*pow(mediumRFeet,2); 
    areaLarge = largeSold*M_PI*pow(largeRFeet,2); 

    // Print solution 
    double dough; 
    dough = areaSmall+areaMedium+areaLarge; 
    double total_dough; 
    total_dough = dough * DOUGH_PER_SQFT; 

    printf("The total amount of dough you need to order this week is", total_dough); 

    return 0; 
} 

我在做什么错?

+0

我希望较小的一段代码,不为你工作,你尝试过什么更好的标题和更多,以及它如何失败。 – 2014-09-21 16:43:13

回答

6
const int DOUGH_PER_SQFT = 0.75; 

这是价值为int和INT的不是浮点数,所以这相当于:

0 

这基本上意味着我n您的最终方程

total_dough = dough * DOUGH_PER_SQFT; 

它将评估为0,因为每平方英尺面团等于0

这可以通过更改纠正:

const int DOUGH_PER_SGFT = 0.75 

到:

const double DOUGH_PER_SGFT = 0.75 
+0

请注意,Clang警告这个:*警告:从'double'到'int'的隐式转换将值从0.75改变为0 *。使用列表初始化会导致错误。 – chris 2014-09-21 15:01:46

+0

谢谢。结合上面的答案,就是这样。现在我只需要输出来匹配676.751417 .... lbs的测试用例答案。那里有任何想法?我得到471.238? – corvette1 2014-09-21 15:20:16

+0

尝试将#define M_PI 3.14159265358979323846更改为const double M_PI = 3.14159265358979323846 – Chantola 2014-09-21 15:25:41

3

打印代码为打印格式为,您显然忘记了参数total_dought的格式。

我建议使用编译器标志来生成警告,如果你使用gcc,add -Wall。

关于解决方案,替换:

printf("The total amount of dough you need to order this week is", total_dough); 

通过

printf("The total amount of dough you need to order this week is %f", total_dough); 
+0

谢谢,学习python之后的第一个程序... – corvette1 2014-09-21 15:18:19

0

有两个问题:

const int DOUGH_PER_SQFT = 0.75; 

的整数着店小数所以它转换成宏:

#define DOUGH_PER_SQFT 0.75 

而且

printf("The total amount of dough you need to order this week is", total_dough); 

不打印的价值。它转换为:

printf("The total amount of dough you need to order this week is%f", total_dough); 

正在运行的代码是:http://ideone.com/aucOlW