所以,我的简单程序计算员工总薪水200美元+佣金。该佣金占其总销售额的9%。因此,该计划要求员工的总销售额,并一直询问,直到我使用哨兵停止。
然后,我显示在一定范围内收到工资的人数,然后使用星号标记在该范围内收到多少人的工资。最后,我还通过添加所有佣金来添加收到的工资和总佣金来显示总工资。代码如下:
#include <iostream>
#include <cmath>
using namespace std;
const int SENTINEL = -1;
int main(){
int salary = 200, total_salary_employee, total_salary=0, gross, commission, total_commission=0, counter[9];
int i;
while(gross != SENTINEL){
cout << "Enter gross sale for employee (-1 to stop): ";
cin >> gross;
if(gross == SENTINEL) break;
if(gross < SENTINEL){
cout << "\nError!\nPlease enter a correct value.\n" << endl;
continue;
}
else{
commission = 0.09 * gross;
total_salary_employee = salary + commission;
total_salary += total_salary_employee;
total_commission += commission;
if((total_salary > 200) && (total_salary < 300)){
counter[1] += 1;
}
else if((total_salary >= 300) && (total_salary < 400)){
counter[2] += 1;
}
else if((total_salary >= 400) && (total_salary < 500)){
counter[3] += 1;
}
else if((total_salary >= 500) && (total_salary < 600)){
counter[4] += 1;
}
else if((total_salary >= 600) && (total_salary < 700)){
counter[5] += 1;
}
else if((total_salary >= 700) && (total_salary < 800)){
counter[6] += 1;
}
else if((total_salary >= 800) && (total_salary < 900)){
counter[7] += 1;
}
else if((total_salary >= 900) && (total_salary < 1000)){
counter[8] += 1;
}
else{
counter[9] += 1;
}
}
};
cout << "----------------------------------------------------------------------";
cout << "\n" << endl;
cout << "Element\t\t" << "# of Persons\t\t" << "Histogram";
cout << "\n";
cout << "$200 - $299\t\t" << counter[1] << "\t\t" << for(i = 0; i < counter[1]; i++){ cout << "*"; };
cout << "\n";
cout << "$300 - $399\t\t" << counter[2] << "\t\t" << for(i = 0; i < counter[2]; i++){ cout << "*"; };
cout << "\n";
cout << "$400 - $499\t\t" << counter[3] << "\t\t" << for(i = 0; i < counter[3]; i++){ cout << "*"; };
cout << "\n";
cout << "$500 - $599\t\t" << counter[4] << "\t\t" << for(i = 0; i < counter[4]; i++){ cout << "*"; };
cout << "\n";
cout << "$600 - $699\t\t" << counter[5] << "\t\t" << for(i = 0; i < counter[5]; i++){ cout << "*"; };
cout << "\n";
cout << "$700 - $799\t\t" << counter[6] << "\t\t" << for(i = 0; i < counter[6]; i++){ cout << "*"; };
cout << "\n";
cout << "$800 - $899\t\t" << counter[7] << "\t\t" << for(i = 0; i < counter[7]; i++){ cout << "*"; };
cout << "\n";
cout << "$900 - $999\t\t" << counter[8] << "\t\t" << for(i = 0; i < counter[8]; i++){ cout << "*"; };
cout << "\n";
cout << "$1000 and above\t\t" << counter[9] << "\t\t" << for(i = 0; i < counter[9]; i++){ cout << "*"; };
cout << "\nTotal salary: " << total_salary;
cout << "\nTotal commission: " << total_commission;
return 0;
}
问题我说:
[错误]在&#39;
之前预期的主要表达式
和
预计[错误]&#39;;&#39;之前&#39;为&#39;
和
预计[错误]&#39;;&#39;之前&#39;)&#39;令牌
预期产出:
元素..............#of person ..............直方图
$ 200- $ 299 ............... 8 ........................... * *******
并且列表继续这样。数字8只是一个例子,点是显示输出是表格形式我不知道如何使用这个编辑器正确地分隔它们。
你能告诉我我的错误在哪里以及我的错误是什么?什么是更好,更短的方式来写这个?
for
循环的编辑代码:
cout << "$200 - $299\t\t" << counter[1] << "\t\t";
for(int i = 0; i < counter[1]; i++){ cout << "*"; };
cout << "\n";
cout << "$300 - $399\t\t" << counter[2] << "\t\t";
for(int i = 0; i < counter[2]; i++){ cout << "*"; };
cout << "\n";
cout << "$400 - $499\t\t" << counter[3] << "\t\t";
for(int i = 0; i < counter[3]; i++){ cout << "*"; };
cout << "\n";
cout << "$500 - $599\t\t" << counter[4] << "\t\t";
for(int i = 0; i < counter[4]; i++){ cout << "*"; };
cout << "\n";
cout << "$600 - $699\t\t" << counter[5] << "\t\t";
for(int i = 0; i < counter[5]; i++){ cout << "*"; };
cout << "\n";
cout << "$700 - $799\t\t" << counter[6] << "\t\t";
for(int i = 0; i < counter[6]; i++){ cout << "*"; };
cout << "\n";
cout << "$800 - $899\t\t" << counter[7] << "\t\t";
for(int i = 0; i < counter[7]; i++){ cout << "*"; };
cout << "\n";
cout << "$900 - $999\t\t" << counter[8] << "\t\t";
for(int i = 0; i < counter[8]; i++){ cout << "*"; };
cout << "\n";
cout << "$1000 and above\t\t" << counter[9] << "\t\t";
for(int i = 0; i < counter[9]; i++){ cout << "*"; };
回答。谢谢大家。
答案 0 :(得分:2)
问题在于您尝试cout
for
循环:
cout << "$200 - $299\t\t" << counter[1] << "\t\t" << for(i = 0; i < counter[1]; i++){ cout << "*"; };
......但是不可能这样做。相反,断开输出 - 可以使用cout << "something";
输出单个字符串,for
循环内的任何内容都可以单独输出,如下例所示:
#include <iostream>
int main()
{
cout << "Hello: ";
for(int i = 0; i < 10; ++i) { cout << "*"; };
}
您可以在线here运行此示例。
答案 1 :(得分:0)
for循环不能用作变量。
替换
cout << "$200 - $299\t\t" << counter[1] << "\t\t" << for(i = 0; i < counter[1]; i++){ cout << "*"; };
与
cout << "$200 - $299\t\t" << counter[1] << "\t\t"; for(i = 0; i < counter[1]; i++){ cout << "*"; };
答案 2 :(得分:0)
C ++中的数组索引从零开始,对于N
元素数组,有效索引为0..N-1
。
您已将阵列定义为
int counter[9];
您的数组中包含9
元素,因此有效索引为0..8
,在您使用的代码1..9
中。索引9
超出了数组的末尾,读取或写入数组的末尾是未定义的行为。
您需要将代码更改为仅使用索引0..8
。
答案 3 :(得分:0)
所以,我在代码中做了很多错误,但感谢GoBusto的回答。
这是经过编辑的,最终的和有效的代码:
#include <iostream>
#include <cmath>
using namespace std;
const int SENTINEL = -1;
int main(){
int total_salary_employee = 0, total_salary = 0, gross, commission, total_commission=0, counter[9] = {0};
while(gross != SENTINEL){
cout << "Enter gross sale for employee (-1 to stop): ";
cin >> gross;
if(gross == SENTINEL) break;
if(gross < SENTINEL){
cout << "\nError!\nPlease enter a correct value.\n" << endl;
continue;
}
else{
commission = 0.09 * gross;
total_salary_employee = 200 + commission;
total_salary += total_salary_employee;
total_commission += commission;
if((total_salary_employee > 200) && (total_salary_employee < 300)){
counter[1] += 1;
}
else if((total_salary_employee >= 300) && (total_salary_employee < 400)){
counter[2] += 1;
}
else if((total_salary_employee >= 400) && (total_salary_employee < 500)){
counter[3] += 1;
}
else if((total_salary_employee >= 500) && (total_salary_employee < 600)){
counter[4] += 1;
}
else if((total_salary_employee >= 600) && (total_salary_employee < 700)){
counter[5] += 1;
}
else if((total_salary_employee >= 700) && (total_salary_employee < 800)){
counter[6] += 1;
}
else if((total_salary_employee >= 800) && (total_salary_employee < 900)){
counter[7] += 1;
}
else if((total_salary_employee >= 900) && (total_salary_employee < 1000)){
counter[8] += 1;
}
else{
counter[9] += 1;
}
}
};
cout << "----------------------------------------------------------------------";
cout << "\n" << endl;
cout << "Element\t\t" << "# of Persons\t\t" << "Histogram";
cout << "\n";
cout << "$200 - $299\t\t" << counter[1] << "\t\t"; for(int i = 1; i <= counter[1]; i++){ cout << "*"; };
cout << "\n";
cout << "$300 - $399\t\t" << counter[2] << "\t\t"; for(int i = 1; i <= counter[2]; i++){ cout << "*"; };
cout << "\n";
cout << "$400 - $499\t\t" << counter[3] << "\t\t"; for(int i = 1; i <= counter[3]; i++){ cout << "*"; };
cout << "\n";
cout << "$500 - $599\t\t" << counter[4] << "\t\t"; for(int i = 1; i <= counter[4]; i++){ cout << "*"; };
cout << "\n";
cout << "$600 - $699\t\t" << counter[5] << "\t\t"; for(int i = 1; i <= counter[5]; i++){ cout << "*"; };
cout << "\n";
cout << "$700 - $799\t\t" << counter[6] << "\t\t"; for(int i = 1; i <= counter[6]; i++){ cout << "*"; };
cout << "\n";
cout << "$800 - $899\t\t" << counter[7] << "\t\t"; for(int i = 1; i <= counter[7]; i++){ cout << "*"; };
cout << "\n";
cout << "$900 - $999\t\t" << counter[8] << "\t\t"; for(int i = 1; i <= counter[8]; i++){ cout << "*"; };
cout << "\n";
cout << "$1000 and above\t\t" << counter[9] << "\t\t"; for(int i = 1; i <= counter[9]; i++){ cout << "*"; };
cout << "\n";
cout << "\nTotal salary: " << total_salary;
cout << "\nTotal commission: " << total_commission;
return 0;
}
我在计数器检查中搞砸了,我把total_salary
置于total_salary_employee
的位置,我还将我的计数器数组初始化为0
。最后,我决定放弃工资变量,只需向佣金增加200。感谢所有试图帮助的人!非常感谢。