gpt4 book ai didi

c - 输出返回错误结果

转载 作者:行者123 更新时间:2023-11-30 18:32:29 26 4
gpt4 key购买 nike

#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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com