首页 文章

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

提问于
浏览
0

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

# 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 回答

  • 2

    有一些问题,

    给大多数汽车功能的

    • 参数都是无用的
      未提供

    • 个必需参数( carCalculate

    • = 0 上的循环 = (赋值而不是相等测试)

    • 在变量未更改的情况下循环

    • 在没有 (param)main() 中调用函数

    这段代码应该有效:

    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 );
    
  • 1

    你没有打电话 . 函数调用需要在函数名后面打开/关闭括号 .

    例如,这不是函数调用:

    functionA;
    

    ......这是一个函数调用:

    functionA();
    

相关问题