我已经创建了一个程序并运行,但是有两个问题。 1)Char不会更改其应有的值。 2)我的总变量之一卡在一个变量上。
我已经多次测试该代码,并且char deptID卡在“ B”上。我尝试过整个工作流程,但始终坚持其价值。为了确保这一点,我编写了一条提示行以在整个工作流程中进行检查。不管我输入什么,它都贴在“ B”上。
2)变量TechTotal似乎停留在1上。我也使用了不同的值对其进行了测试。我还继续使用cout线来确定整个工作流程中的价值,但没有成功。我已经确保变量在计算变量时是正确的。两者都是正确的。
这是我的主要代码:
int main(int argc, char** argv) {
welcomeScreen();
long int empID;
int TechAccAvg, TechTixAvg;
int BusTotal, TechTotal, BusAccAvg, BusTixAvg;
char deptID;
for (int i=0; i < 2; i++)
{
cout << "What department are you apart of?\n";
cin >> deptID;
if (deptID = 'B')
{
auto Averages = gatherData(deptID);
BusTixAvg = std::get<0>(Averages);
BusAccAvg = std::get<1>(Averages);
cout << BusTixAvg << endl;
cout << BusAccAvg << endl;
BusTotal = BusTixAvg + BusAccAvg;
cout << "Bus Total: " << BusTotal << endl;
}
else if (deptID = 'T')
{
auto TechAverages = gatherData(deptID);
TechTixAvg = std::get<0>(TechAverages);
TechAccAvg = std::get<1>(TechAverages);
cout << TechTixAvg << endl;
cout << TechAccAvg << endl;
TechTotal = TechTixAvg + TechAccAvg;
cout << "Tech Total: " << TechTotal << endl;
}
}
cout << "Tech: " << TechTotal << endl;
cout << "Business: " << BusTotal << endl;
summaryReport(TechTotal, BusTotal);
goodByeScreen();
return 0;
}```
` std::tuple<int, int> gatherData (char dept)
{
tuple <double, double> Averages;
int employeeNum=0, TotalTix=0, TotalAcc=0, trafficTickets=0, accidents=0;
long int empID=0;
double TixAverage=0, AccAverage=0;
char deptID;
cout << dept << endl;
cout << "How many employees do you have?\n";
cin >> employeeNum;
for(int j = 0; j < employeeNum; j++)
{
cout << "Please enter your employees ID number\n";
cin >> empID;
cout << "How many tickets did they have this year?\n";
cin >> trafficTickets;
TotalTix += trafficTickets;
cout << "How many accidents did they have this year?\n";
cin >> accidents;
TotalAcc += accidents;
}
TixAverage = TotalTix / employeeNum;
AccAverage = TotalAcc / employeeNum;
cout << "Department: " << dept << endl;
cout << "Total employees: " << employeeNum << endl;
cout << "Total tickets: " << TotalTix << endl;
cout << "Total Accidents: " << TotalAcc << endl;
Averages = make_tuple (TotalTix, TotalAcc);
return Averages;
}```
This is used to create the tuple that is used in determining Totals for both 'B' and 'T' depts.
Fixing both the char dept and the TechTotal would fix the entire program, I think. Those are the only things holding the program back. I've been stuck on this problem for a few hours now and I'm kind of lost as to why it's not changing those values. Any help would be appreciated, thank you in advance!
最佳答案
解
将else if (deptID = 'T')
替换为else if (deptID == 'T')
,将if (deptID = 'B')
替换为if (deptID == 'B')
。
说明
单等号=
表示赋值。因此,每次程序运行时,都会将deptID
分配给B
,并且该语句将返回true
,从而满足if
语句。
但是,您想比较两个值以查看它们是否相等。因此,必须使用==
(等于)。
因为else if
中的语句将永远不会执行,所以TechTotal
将保持未初始化状态,并且该内存地址中的值恰好是1
。