对“函数double getSales(float)已包含主体”感到困惑

时间:2020-07-16 20:56:21

标签: c++ function

有人知道我在做什么错吗?显然我得到了,因为我正在使用相同的名称但参数不同的功能设置多个函数。但是我不明白如何解决这个编程问题。问题指出:

编写一个程序来确定公司的四个部门(东北, 东南,西北和西南)的销售量达到了四分之一。这应该 包括以下两个由main调用的函数。

•double getSales()传递了部门名称。它要求用户提供部门的 季度销售数字,验证输入,然后返回。应该为它调用一次 每个部门。

•void findHighest()传递了四个销售总额。它确定哪个是最大的 并打印高收入部门的名称以及其销售数字。

输入验证:不接受少于$ 0.00的美元金额。

这是我的代码:

#include <iostream>
using namespace std;

double getSales(float);
void findHighest(float, float, float, float);


int main()
{
    float Northeast;
    float Southeast;
    float Northwest;
    float Southwest;
    
    getSales(Northeast);
    getSales(Southeast);
    getSales(Northwest);
    getSales(Southwest);
    findHighest(Northeast, Southeast, Northwest, Southwest);
}

double getSales(float Northeast)
{
    cout << "Enter the quarter sales for the Northeast division: $";
    cin >> Northeast;
    while (Northeast < 0)
    {
        cout << "Invalid input! Please enter a positive number!";
        cin >> Northeast;
    }
    return Northeast;
}

double getSales(float Southeast)
{
    cout << "Enter the quarter sales for the Southeast division: $";
    cin >> Southeast;
    while (Southeast < 0)
    {
        cout << "Invalid input! Please enter a positive number!";
        cin >> Southeast;
    }
    return Southeast;
}

double getSales(float Northwest)
{
    cout << "Enter the quarter sales for the Northwest division: $";
    cin >> Northwest;
    while (Northwest < 0)
    {
        cout << "Invalid input! Please enter a positive number!";
        cin >> Northwest;
    }
    return Northwest;
}

double getSales(float Southwest)
{
    cout << "Enter the quarter sales for the Southwest division: $";
    cin >> Southwest;
    while (Southwest < 0)
    {
        cout << "Invalid input! Please enter a positive number!";
        cin >> Southwest;
    }
    return Southwest;
}

void findHighest(float Northeast, float Southeast, float Northwest, float Southwest)
{
    if (Northeast > Southeast && Northeast > Northwest && Northeast > Southwest)
    {
        cout << "Northeast is the highest grossing division!";
    }
    else if (Southeast > Northeast && Southeast > Northwest && Southeast > Southwest)
    {
        cout << "Southeast is the highest grossing division!";
    }
    else if (Northwest > Northeast && Northwest > Southeast && Northwest > Southwest)
    {
        cout << "Northwest is the highest grossing division!";
    }
    else if (Southwest > Northeast&& Southwest > Southeast&& Southwest > Northwest)
    {
        cout << "Southwest is the highest grossing division!";
    }
    else
    {
        cout << "There is a tie between 2 or more divisions for the highest grossing division!";
    }
}

1 个答案:

答案 0 :(得分:2)

您不能同时拥有double getSales(float Southwest)double getSales(float Northwest)

对于您的程序,它们是同一回事。如果我要写,请在我的主语中:

antartica = 3.0;
double money = getSales(antartica);

编译器应该做什么?可以使用调用者想要的任何名称来调用函数,并通过名称和参数类型来区分它们。您应该可以摆脱一个障碍:

double getSales(const char* areaName)
{
    float area;
    cout << "Enter the quarter sales for the " << areaName << " division: $";
    cin >> area;
    while (area < 0)
    {
        cout << "Invalid input! Please enter a positive number!";
        cin >> area;
    }
    return area;
}

Northeast = getSales("Northeast");
Southeast = getSales("Southeast");
Northwest = getSales("Northwest");
Southwest = getSales("Southwest");

这将大大减少您的代码量,并使调试和维护更加容易。

随着您对编程的进一步了解,您可能会遇到DRY原理:

https://en.wikipedia.org/wiki/Don%27t_repeat_yourself