我无法在多个函数之间传递不同的值。也就是说,我试图让两个函数接受用户输入,然后将这两个输入都传递给第三个函数,该函数计算这些输入的总和。然后,它将总数传递给另一个计算真实总数的函数。最后,在那之后,最后一个功能显示最终编号。我似乎无法弄清楚如何使多个函数通过。
# 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");
}
答案 0 :(得分:2)
有一些问题,
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);
}
在main
中调用的int
carLength( &length );
答案 1 :(得分:1)
您不进行任何函数调用。函数调用必须在函数名称后带有打开/关闭括号。
例如,这不是函数调用:
functionA;
...这是一个函数调用:
functionA();