-1
#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 函数中的计算语句似乎有问题。

4

3 回答 3

2

您需要分配结果getSquareFootage(squareFootage);

squareFootage = getSquareFootage(squareFootage);

由于squareFootage是按值传递而不是按引用传递,换句话说,无论您在函数中对其进行多少更改,它都不会在函数之外产生任何影响。或者,您可以通过引用传递它:

void getSquareFootage(int * squareFootage)
{
     printf("Enter the square footage of the surface: ");
     scanf("%d", squareFootage);
}

这将被称为:

getSquareFootage(&squareFootage);
于 2012-10-21T19:47:32.920 回答
1

像这样正确squareFootage=getSquareFootage();

无需传递参数。

于 2012-10-21T19:48:27.357 回答
1

您没有更新变量 squareFootage。当您调用 calcestpaint(squareFootage) 时,您将值 0 作为参数传递。

于 2012-10-21T22:23:40.620 回答