【发布时间】:2015-02-14 03:58:42
【问题描述】:
我遇到了一个 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;
}
【问题讨论】:
-
诸如`smallfeet = ("%d/12", small)
? Andprintf("You will need to purchase ",dough_needed,"数磅面团等语句背后的目的是什么? ");` 不会打印dough_needed的值。您真的应该阅读 C 书籍或教程。 -
我认为如果您坐下来阅读K&R,您会受益匪浅。它简短易读。