首先让我开始说我是c ++的新手,也是全新的stackoverflow。 大家好,我一直在努力解决这个问题。我正在尝试通过下面函数中的变量“playerTotal”更新变量“real_total”。然而,它所做的只是让它们平等。当我cout<< real_total<< playerTotal<< ENDL;我得到相同的数字,这对于用户的第一次猜测是正确的,但他们的第二次猜测就像“real_total”重置为0而不是保持“playerTotal”的值。我很确定它与我的循环或我的陈述的位置有关。任何帮助将不胜感激。
void update_total (string str, char& letter, int num, bool& roundover, int& playerTotal){
int found = 0, real_total=playerTotal;
for(int i=0; i < str.length(); i++){
if(str.at(i) == letter){
found = found + 1;
}
}
playerTotal = num * found;
real_total =+ playerTotal;
cout << real_total << playerTotal << endl;
cout << "There were " << found << " of those in the phrase. Your total earnings is $" << playerTotal << endl;
cout << "Total game winnings is $ " << real_total << endl;
if(found == 0){
cout << "You guessed incorectly:(" << endl;
roundover = true;
}
}
答案 0 :(得分:1)
对此的简单回答如下:
void foo() {
int i = 0;
int j = 10;
// Increment i with j
i += j;
}
+ =运算符将右操作数的值添加到左操作数,并将左操作数的新值赋给变量。