#include <stdio.h>
#define GA_OF_PA_NEED 267.0
int getSquareFootage(int squareFootage);
double calcestpaint(int squareFootage);
double printEstPaint(double gallonsOfPaint);
int main(void)
{
//Declaration
int squareFootage = 0;
double gallonsOfPaint = 0;
//Statements
getSquareFootage(squareFootage);
gallonsOfPaint = calcestpaint(squareFootage);
gallonsOfPaint = printEstPaint(gallonsOfPaint);
system("PAUSE");
return 0;
}
int getSquareFootage(int squareFootage)
{
printf("Enter the square footage of the surface: ");
scanf("%d", &squareFootage);
return squareFootage;
}
double calcestpaint( int squareFootage)
{
return (double) (squareFootage * GA_OF_PA_NEED);
}
double printEstPaint(double gallonsOfPaint)
{
printf("The estimate paint is: %lf\n",gallonsOfPaint);
return gallonsOfPaint;
}
为什么我的输出显示 gallonsOfPaint 为 0.0,没有错误,一切似乎在逻辑上都是正确的。 calc 函数中的calculate 语句似乎有问题。
最佳答案
您需要分配getSquareFootage(squareFootage);
的结果:
squareFootage = getSquareFootage(squareFootage);
由于 squareFootage
是按值传递的,而不是按引用传递的,换句话说,无论您在函数中对其进行多少更改,它在函数外都不会产生任何影响。或者,您可以通过引用传递它:
void getSquareFootage(int * squareFootage)
{
printf("Enter the square footage of the surface: ");
scanf("%d", squareFootage);
}
将这样调用:
getSquareFootage(&squareFootage);
关于c - 输出返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13001510/