C++ For循环导致程序向计数器添加不正确的数字
我正在做一个程序,计算员工每月的销售数字,并将每个数字与每年的配额数字进行比较。它会对每个员工进行12次比较,并显示哪个员工做得最差,有多少员工错过了至少一个月的数字,以及基于上一次计算的百分比。这是我的密码C++ For循环导致程序向计数器添加不正确的数字,c++,for-loop,counter,C++,For Loop,Counter,我正在做一个程序,计算员工每月的销售数字,并将每个数字与每年的配额数字进行比较。它会对每个员工进行12次比较,并显示哪个员工做得最差,有多少员工错过了至少一个月的数字,以及基于上一次计算的百分比。这是我的密码 #include <iostream> #include <fstream> #include <string> #include <conio.h> #include <iomanip> using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <iomanip>
using namespace std;
int main()
{
int numEmployee = 0;
string employeeName;
int yearQuota = 0;
int monthQuota = 0;
int quotaMissed = 0;
int quotaMet = 0;
int maxMissed = 0;
string maxEmployee;
int missedAmount = 0; //Amount of employee's that missed at least one quota
bool missedOne = false;
double quotaPercent = 0;
ifstream reader ("data.txt");
cout << "Employee Name" << " Yearly Quota" << " Monthly Sales" << "\n\n";
reader >> numEmployee;
for (int i = numEmployee; i > 0; i--)
{
reader >> employeeName;
cout << " " << employeeName;
reader >> yearQuota;
cout << " " << yearQuota;
cout << " ";
for (int b = 12; b > 0; b--)
{
reader >> monthQuota;
if (monthQuota < yearQuota)
{
quotaMissed += 1;
missedOne = true;
}
else
{
quotaMet += 1;
}
cout << monthQuota << " ";
reader.clear();
}
if (missedOne)
{
missedAmount += 1;
}
cout << quotaMissed << " ";
cout << quotaMet << " ";
if (quotaMissed > maxMissed)
{
maxMissed = quotaMissed;
maxEmployee = employeeName;
}
cout << "\n\n";
}
cout << "\n\n";
quotaPercent = static_cast<double>(missedAmount) / numEmployee;
quotaPercent *= 100;
if (quotaPercent > 20)
{
cout << "A Sell It! Or Else Campaign Needs To Be Initiated!";
cout << quotaPercent << "%" << " of the employee's didn't meet their quota's";
cout << "Consider Firing: " << maxEmployee;
}
else
{
cout << "A Sell It! Or Else Campaign Doesn't Need To Be Initiated!";
cout << "only " << quotaPercent << "%" << " of the employee's didn't meet their quota's";
}
_getch();
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
int numEmployee=0;
字符串employeeName;
国际年配额=0;
int monthQuota=0;
int quotamised=0;
int quotaMet=0;
int=0;
字符串maxEmployee;
int missedAmount=0;//至少错过一个配额的员工数量
bool-missdone=false;
双引号=0;
ifstream阅读器(“data.txt”);
cout>employeeName;
不能使用配额;
每月定额;
如果(月配额<年配额)
{
QUOTAMISED+=1;
未完成=正确;
}
其他的
{
quotaMet+=1;
}
难道我不知道业务逻辑,但从你的帖子和变量名称来看,我有一个问题。你说monthQuota
你不应该把monthQuota
加起来12个月,然后与yearQuota
比较吗?monthQuota
无论如何都小于yearQuota
(逻辑上,仅按变量名)所以所有员工都会点击missedOne=true;
,并且missedAmount+=1
已经完成。所以100%未命中配额。P.S完全是猜测工作。我不知道业务逻辑,所以我所说的完全没有意义:)你听起来像是在描述这两个cout