c - 输出返回错误结果

标签 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 语句似乎有问题。

最佳答案

您需要分配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/

相关文章:

c++ - 未正确定义字节序宏

javascript - 使用JS函数代替重复代码

php - 修复 PHP 空函数

c - C中奇怪的初始化

python - 正确地为 c 包装器制作 setup.py

javascript - 在 Nightwatch 页面对象中使用 windowHandles

javascript - 如果焦点不在输入上,则隐藏搜索结果

mysql - 比较 mysql 表并更新值

c - int * p 和 int p* 有什么区别?

c - 生成编译 gcc 的文件并将对象放入单独的文件夹中