C程序在第一次输入时崩溃

时间:2015-02-14 03:58:42

标签: c crash

我遇到第一个用户输入崩溃的C程序问题。我认为应该是对的;我遇到的问题是在没有给出错误的情况下正常运行,但是只要你给出第一个输入,它就会立即崩溃。有什么想法吗?

# define DOUGH_PER_SQFT 0.75
# define INCHES_PER_FEET 12
# define PI 3.141592654

# include <stdio.h>
# include <math.h>

int main() {

    // Get user inputs.

    int small, medium, large;

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

    int smallfeet, mediumfeet, largefeet;

    smallfeet = ("%d/12", small);
    mediumfeet = ("%d/12", medium);
    largefeet = ("%d/12", large);

    // Find the amount of dough, in pounds, that will need to be ordered

    int surface_small, surface_medium, surface_large;

    surface_small = (PI*pow(smallfeet,2));
    surface_medium = (PI*pow(mediumfeet,2));
    surface_large = (PI*pow(largefeet,2));

    int smallweight, medweight, largeweight, doughneeded;

    smallweight = surface_small*DOUGH_PER_SQFT;
    medweight = surface_medium*DOUGH_PER_SQFT;
    largeweight = surface_large*DOUGH_PER_SQFT;

    int dough_for_smalls, dough_for_mediums, dough_for_larges, dough_needed;

    dough_for_smalls = smallweight*small;
    dough_for_mediums = medweight*medium;
    dough_for_larges = largeweight*large;
    dough_needed = dough_for_smalls + dough_for_mediums + dough_for_larges;

    // Find the answer.

    printf("You will need to buy ",dough_needed,"pounds of dough for this week");

    return 0;

}

2 个答案:

答案 0 :(得分:4)

您需要传递scanf

变量的地址
scanf("%d", &small);

与其他人一样

答案 1 :(得分:1)

更改

scanf("%d", small); //and
scanf("%d", medium); //and
scanf("%d", large);

scanf("%d", &small); //and
scanf("%d", &medium); //and
scanf("%d", &large);

这样做是因为%d中的scanf格式说明符需要int*,但是您提供了导致崩溃的int

代码中还存在其他问题。要修复它们,请更改

smallfeet = ("%d/12", small);
mediumfeet = ("%d/12", medium);
largefeet = ("%d/12", large);

smallfeet = small/12;
mediumfeet = medium/12";
largefeet = large/12;

printf("You will need to buy ",dough_needed,"pounds of dough for this week");

printf("You will need to buy %d pounds of dough for this week",dough_needed);