输出返回错误的结果

时间:2012-10-21 19:44:24

标签: c function

#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语句有问题。

3 个答案:

答案 0 :(得分:2)

您需要指定getSquareFootage(squareFootage);的结果:

squareFootage = getSquareFootage(squareFootage);

由于squareFootage是按值传递的,而不是通过引用传递的,换句话说,在函数中更改它并不重要,它在函数外部没有任何影响。或者,您可以通过引用传递它:

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

将被这样调用:

getSquareFootage(&squareFootage);

答案 1 :(得分:1)

更正squareFootage=getSquareFootage();

无需传递参数。

答案 2 :(得分:1)

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

相关问题