c – 如何将引用变量增加1

我是c的新手,我正在尝试从50开始增加汽车,但如果你的损失比油耗更大,那么只增加1.我希望汽车在下一次循环时保持其价值.我希望这是有道理的.
int Power (int &car);

int main(){
    int car = 50;
    // ...
    // ...
    // ...

    int carDamage = 0;
    int yourDamage = 0;
    // pick a random number between 1 to 50
    yourDamage = 0 + rand() % (50 - 0 + 1);
    carDamage = 0 + rand() % (50 - 0 + 1);
    cout << "You hit the car and cause damage of: " << carDamage << endl;
    cout << "The car hits you and causes damage of: " << yourDamage << endl;
    cout << endl;

    if(carDamage < yourDamage)
    {
        cout << "You win" << endl;
        cout << "You gain strength." << endl;
        cout << endl;
        int car = car + 1;
    }
    else
    {

解决方法

你正在声明一个新变量遮蔽原始变量.

更改

int car = car + 1;

car = car + 1;

相关文章

首先GDB是类unix系统下一个优秀的调试工具, 当然作为debug代...
1. C语言定义1个数组的时候, 必须同时指定它的长度.例如:int...
C++的auto关键字在C+⬑新标准出来之前基本...
const关键字是用于定义一个不该被改变的对象,它的作用是告诉...
文章浏览阅读315次。之前用C语言编过链表,这几天突然想用C+...
文章浏览阅读219次。碰到问题就要记录下来,防止遗忘吧。文章...