【问题标题】:How to use functions return in another function in C?如何在C中的另一个函数中使用函数返回?
【发布时间】:2021-06-24 02:13:52
【问题描述】:

我的这个函数返回billType,我想在我的主函数中使用这个billType

int bill()
{
    int billType;
    printf("Enter bill type (1=utilities, 2=internet) : ");
    scanf("%d",&billType);
    return billType;
}

这里主要功能:

int main()
{
    int discount;

    bill();

    if(billType == 1)
    {
        int choice;
        printf("\nEnter 1=TNB or 2=SYABAS: ");
        scanf("%d", &choice);

        if(choice == 1)
        {
            discount= 0.05;
        }

        else if(choice == 2)
        {
            discount= 0.10;
        }
    }

    if(billType == 2)
    {
        int choice;
        printf("\nEnter 1=DIGI, 2=UNIFI or 3=MAXIS FIBRE : ");
        scanf("%d", &choice);

        discount = 0.05;
    }
}

【问题讨论】:

    标签: c function


    【解决方案1】:

    您需要将返回值分配给一个变量以供以后使用。

    你应该改变

    bill();
    

    int billType = bill();
    

    另外,将诸如0.100.05 之类的小值分配给int discount; 看起来很奇怪,因为它们将被截断为零。考虑使用其他类型(例如,double)或删除无意义的行(因为未读取discount)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 2015-02-05
      • 2022-11-23
      • 2017-03-29
      • 1970-01-01
      相关资源
      最近更新 更多