如何总计输出

时间:2017-04-17 08:25:01

标签: c++

我无法输出销售总数,所需的输出必须如下:

推销员1销售额:20000销售额15%

推销员2销售额:15000销售额15%

推销员3销售额:10000销售额13%

推销员4销售额:5000销售额13%

推销员5销售额:12000销售额13%

推销员6销售额:25000销售额20%

总销售额:

20% - 1

15% - 2

13% - 3

这是我的代码:

int a = 0;
int b = 0;
int c = 0;
for (int x = 1; x <= salesMan; x++)
        {
            if (sales[x] > 20000)
            {
                a = x + a;

                cout << "Salesman " << x << " Sales: " << sales[x] << "  20%";
                cout << "\n";
            }
            else if (sales[x] <= 20000 && sales[x] >= 15000)
            {
                b = x + b;

                cout << "Salesman " << x << " Sales: " << sales[x] << "  15%";
                cout << "\n";
            }
            else if (sales[x] < 15000 && sales[x] >= 0)
            {
                c = x + c;
                cout << "Salesman " << x << " Sales: " << sales[x] << "  13%";
                cout << "\n";
            }
        }

    cout <<"20% - "<< a <<"\n";
    cout <<"15% - "<< b <<"\n";
    cout <<"13% - "<< c <<"\n";

解: a += 1不是a = x + a&lt; ---此代码会添加推销员的索引,这就是我无法得到答案的原因。

谢谢@TZHX

1 个答案:

答案 0 :(得分:0)

你需要增加a,b,c。像

a=a+1;
b=b+1;
c=c+1;

目前您正在添加索引,这会给您错误的输出。