打印解决方案(C新手)

时间:2014-09-21 14:53:25

标签: c

我无法弄清楚为什么我的程序不打印最终解决方案(totalDough)。输入是8,10,12然后是40,100和200:

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

我做错了什么?

3 个答案:

答案 0 :(得分:6)

const int DOUGH_PER_SQFT = 0.75;

这是值int和int不是浮点数,因此转换为:

0

这基本上意味着在你的最终等式中

total_dough = dough * DOUGH_PER_SQFT;

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

可以通过更改:

来更正
const int DOUGH_PER_SGFT = 0.75

成:

const double DOUGH_PER_SGFT = 0.75

答案 1 :(得分:3)

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

我建议使用生成警告的编译器标志,如果你使用gcc,添加-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);

答案 2 :(得分:0)

有两个问题:

const int DOUGH_PER_SQFT = 0.75;

整数cant存储小数,因此将其转换为宏:

#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