C ++ Bool函数

时间:2018-03-26 06:03:42

标签: c++

Option Explicit Sub test() StartDate = Format(Cells(11, 4).Value, "yyyy-mm-dd") EndDate = Format(Cells(12, 4).Value, "yyyy-mm-dd") If EndDate < StartDate Then MsgBox "End Date is earlier than StartDate. Please select an end date after the start date." Exit Sub End If For Each WS In Worksheets If WS.Name Like "WS_Name" Then Exist = True Exit For End If Next If Exist Then ThisWorkbook.Sheets("WS_Name").Cells.ClearContents With 'some code that uses the StartDate and EndDate in a SQL query in a database End With Else ActiveWorkbook.Worksheets.Add.Name = "WS_Name" With 'some code that uses the StartDate and EndDate in a SQL query in a database End With End If End Sub 函数有点问题。无论我输入的薪水如何,第二句都打印出来。我是编程新手。

我的任务:

  

定义一个函数,决定某人是否可以根据他/她的工资租用公寓。

     

租金最多应为薪水的三分之一。如果超过三分之一,客户的申请将被拒绝。

     

该功能的目的是决定客户是否合格。

这是我的问题孩子:

bool

以下是'canbuy'的函数定义:

if (age > 17)
{               
    if (canbuy(salary, rent))
    {
        cout << "Based on the information you have provided, you are qualified. You are old enough and have money." << endl << endl;
    }
    else
    {
        cout << "Based on the information you have provided, you are not qualified. Sorry... Make more money next time.  " << endl << endl;
    }
}

结果的视觉示例: https://i.imgur.com/WE28e1H.png

以下是整个计划:

bool canbuy(double salary, double rent)
{
    if (rent <= ((1 / 3)* salary))
    {
        return true;
    }    
    else
    {
        return false;
    }

    return canbuy;
}

1 个答案:

答案 0 :(得分:0)

我对该程序进行了一些测试,并且您的canbuy函数中存在逻辑错误。

正如Saliesh D在评论前面所述,在if rent( <= ((1/3) * salary))行中,1和3被视为整数。因此,它将截断您的十进制结果并舍入为最小整数。在你的情况下,1/3将评估为0。

在测试用例中,我有两个整数,a = 1b = 3。在a / b时,结果输出结果为0

要解决此问题,您只需执行

即可将数字指定为浮点数

if (rent <= ((1.0/3.0) * salary))