gpt4 book ai didi

c++ - 如何更改字符的值?

转载 作者:行者123 更新时间:2023-11-28 04:09:50 25 4
gpt4 key购买 nike

我已经创建了一个程序并且它运行了,但是有两个问题。 1) Char 不会按应有的方式更改值。 2)我的总变量之一卡在了一个上。

我已经多次测试代码,char deptID 卡在“B”上。我已经尝试过整个工作流程,但它停留在值(value)上。为了确保,我写了一条 cout 行来在整个工作流程中对其进行检查。无论我输入什么,它都停留在“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

关于c++ - 如何更改字符的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58045142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com