我在多个函数之间传递不同的值时遇到问题 . 也就是说,我试图让两个函数接受用户输入,然后将这两个输入传递给第三个函数,该函数计算添加在一起的那些输入 . 然后,它将该总数传递给另一个计算真实总数的函数 . 最后,在此之后,最后一个函数显示最终的数字 . 我似乎无法弄清楚如何让多个函数通过 .
# 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");
}
2 回答
有一些问题,
给大多数汽车功能的
参数都是无用的
未提供
个必需参数(
carCalculate
)= 0
上的循环=
(赋值而不是相等测试)在变量未更改的情况下循环
在没有
(param)
的main()
中调用函数这段代码应该有效:
例如,您也可以要求输入函数填充一个指针作为参数给出的值
int在
main
中调用你没有打电话 . 函数调用需要在函数名后面打开/关闭括号 .
例如,这不是函数调用:
......这是一个函数调用: