似乎无法计算正确的值

时间:2011-10-31 22:47:15

标签: c++

大家好我正在学习函数,所以我采用了旧的练习程序并对其进行了修改以使用函数。然而,我的问题是,当显示我正在计算的佣金时,我得到了错误的值。任何帮助,将不胜感激。除了佣金价格外,我的一切都在运作。

代码

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

int ID;
double salary, pcs, printer, accessory, maint;
double pcbase = 8000.00, printerbase = 1500.00, accbase = 1000.00, maintbase = 500.00;
double pccomm = .15, printercomm = .10,acccomm = .05, maintcomm = .08;

double calcComm(double sales, double base, double percent);
void display(double, double, double, double);


int main()
{
    cout<<"Enter Salesman ID\n";
    cin>>ID;
    while (ID != -999)
    {
        cout<<"Enter base salary\n";
        cin>>salary;
        cout<<"Enter PC sales\n";
        cin>>pcs;
        pccomm = calcComm(pcs, pcbase, pccomm);

        cout<<"Enter Printer Sales\n";
        cin>>printer;
        printercomm = calcComm(printer, printerbase, printercomm);

        cout<<"Enter Accessory Sales\n";
        cin>>accessory;
        acccomm = (accessory, accbase, acccomm);

        cout<<"Enter Maintenance Sales\n";
        cin>>maint;
        maintcomm = (maint, maintbase, maintcomm);

        display(pccomm, printercomm, acccomm, maintcomm);
        cout<<"\n\nEnter Salesman ID or -999 to quit\n";
        cin>>ID;
    }

    system ("PAUSE");
    return 0;
}

double calcComm(double sales, double base, double percent)
{
    double commission;
    if (sales < base)
        commission = 0;
    else 
        commission = sales * percent;
    return commission;
}

void display(double pccomm, double printercomm, double acccomm, double maintcomm)
{
    cout<<"Salesman ID:"<<ID;
    cout<<"\nProduct"<<"            Sales Amount"<<"      Commission\n";
    cout<<"Personal Computers"<<"     "<<pcs<<"               "<<pccomm;
    cout<<"\nPrinters"<<"               "<<printer<<"               "<<printercomm;
    cout<<"\nAccessories"<<"            "<<accessory<<"               "<<acccomm;
    cout<<"\nMaintenance"<<"            "<<maint<<"               "<<maintcomm;

    double total = 0,totaldue = 0;
    total = pccomm+printercomm+acccomm+maintcomm;
    totaldue = total+salary;
    cout<<"\n\n\nTotal Commission                               "<<total;
    cout<<"\n       Base Pay                                "<<salary;
    cout<<"\n      Total Due                                "<<totaldue;
}

我想要的输出是

Salesman ID: 111
Product                   Sales             Commission
Personal Computer         5000.00                0.00
Printers                  2000.00               50.00
Accessories                600.00                0.00
Mainteance                1000.00               40.00

Total Commission                                90.00
Base Pay                                      1234.56
Total Due                                     1324.56

3 个答案:

答案 0 :(得分:2)

你有:

acccomm = (accessory, accbase, acccomm);

我认为这必须是acccomm = calcComm(accessory, accbase, acccomm);

计算maintcomm时相同。编写代码的方式,它将使用comma operator,它只是评估最后一个操作数。

答案 1 :(得分:0)

这是一个疯狂的猜测,因为你没有包含任何样本输入或输出。在达到基数之前不支付佣金是不寻常的,但如果达到基数则按照整个销售数字计算支付佣金。

你的意思是

commission = (sales-base) * percent;

而不是

commission = sales * percent;

答案 2 :(得分:0)

佣金=销售额*基数*百分比?

您需要解释“基础”变量是什么?是单价吗?佣金前的金额?我假设销售额是单位数,百分比是显而易见的。