gpt4 book ai didi

c - 在 C 中的多个函数之间传递不同的值?

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:20 27 4
gpt4 key购买 nike

我在多个函数之间传递不同的值时遇到问题。也就是说,我试图让两个函数接受用户输入,然后将这两个输入传递给第三个函数,该函数计算这些输入的总和。之后,它将总数传递给另一个计算真实总数的函数。最后,在那之后,最后一个函数显示最终数字。我似乎无法弄清楚如何让多个函数通过。

# include <stdio.h>
# include <stdlib.h>

int carLength(int userInput) //Length
{
int length = 0;
do
{
printf("\nPlease input the length of the area being carpeted:");
scanf("%d", &userInput);
} while (length = 0);
length = userInput; //Essentially using this to store user input so i can
use it later.
return length;
}

int carWidth(int userInput) //Width
{
int width = 0;
do
{
printf("\nPlease input the width of the area being carpeted:");
scanf("%d", &userInput);
} while (width = 0);
width = userInput; //Same as width
return width;
}

int carCalculate(int cost) //cost
{
int width = userInput;
int cost = (width * length) * 20;
return cost;
}

float cardiscount(float discount) //discount
{
float discount = cost - (cost * .1);
return discount;
}

float displaydisc(float discount) //discount
{
printf("\nThe total cost is: %f\n", discount);
return discount;
}

int main()
{
int length = 0;
int width = 0;
int cost = 0;
int discount = 0;
do {
length = carLength;
} while (length == 0);
do {
width = carWidth;
} while (carWidth == 0);
do {
cost = carCalculate;
} while (cost == 0);
do {
discount = cardiscount;
} while (discount == 0);
do {
displaydisc;
} while (discount > 0);
printf("\n Thank you for using this program!");

system("pause");
}

最佳答案

有几个问题,

  • 大多数汽车功能的参数都是无用的
  • 未提供所需参数(carCalculate)
  • = 0 上的循环有 =(赋值而不是等式测试)
  • 在变量不变的条件下循环
  • main() 中调用函数而不使用 (param)

这段代码应该可以工作:

int carLength() // <== no need to provide length, it is returned
{
int length;
do
{
printf("\nPlease input the length of the area being carpeted:");
scanf("%d", &length);
} while (length == 0); // <== length is changed within the loop
return length;
}

int carWidth() //Width
{
int width = 0;
do
{
printf("\nPlease input the width of the area being carpeted:");
scanf("%d", &width);
} while (width == 0);
return width;
}

int carCalculate(int width, int length) // <== need to provide values
{
int cost = (width * length) * 20;
return cost;
}

float cardiscount(float cost) //discount
{
float discount = cost - (cost * .1);
return discount;
}

void displaydisc(float discount) //discount
{
printf("\nThe total cost is: %f\n", discount);
}

int main()
{
int length; // <== no need to set to 0, as their values
int width; // are going to be initialized later
int cost;
int discount;

length = carLength();

width = carWidth();

cost = carCalculate(width, length);

discount = cardiscount((float)cost);

displaydisc(discount);

printf("\n Thank you for using this program!");

system("pause");
}

您也可以要求输入函数填充一个值,该值将指针作为参数给出,例如

int carLength(int *length) {
do {
printf("\nPlease input the length of the area being carpeted:");
scanf("%d", length);
} while (*length == 0);
}

int 在 main 中调用

carLength( &length );

关于c - 在 C 中的多个函数之间传递不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53699303/

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