将If语句添加到我的函数中

时间:2018-02-14 02:42:09

标签: c++

刚刚开始编码,并有一个分配,提示用户输入"工资"和"小时"然后计算。我们开始使用函数,我编写了一个调用代码的代码,但是我得到一个错误,说我的函数中的if语句永远不会被执行。这是以下代码:

#include <iostream>
#include <iomanip>
using namespace std;

float calcwage (float h, float w);

int main()
{
    float wage, hours, totwage;
    cout << setiosflags(ios::fixed|ios::showpoint)
    << setprecision(2);
    cout << "Enter hours worked: ";
    cin >> hours;
    cout << "Enter hourly wage:$ ";
    cin >> wage;
    totwage = calcwage (hours, wage);
    cout <<"Total wages=$ " << totwage << "\n";
    return 0;
}

float calcwage (float h, float w)
{
    return h * w; if(h <= 40)
        ;
    return (h * (w * 1.5)); if(h > 40)
        ;
}

函数中是否不允许使用if语句?我很困惑,也不确定我的代码如何返回main。

1 个答案:

答案 0 :(得分:-1)

你的if语句需要在return语句之前。

此外,您只需要if语句,因为只有两个选项。如果第一个没有被击中而不需要检查另一个选项作为唯一的其他选项。

if(h <= 40)返回h * w;

返回h * w * 1.5

相关问题