C ++ - 变量未更新

时间:2016-12-27 17:15:42

标签: c++ variables

我是新来的,也是编程的新手,所以在我的第一课我做了下面的代码,一切都很顺利,除了最后一个“总数武器”,当我在numberPistols中添加+1时没有更新。我知道numberPistols已更新,但在最后一行使用numberWeapons时,它不会显示新值。

我仍然无法理解为什么,有人可以向我解释并向我展示我做错了吗?

#include <iostream>
using namespace std;

int main() {
    int numberPistols = 5;
    int numberKnives = 18;
    int numberWeapons = numberPistols + numberKnives;

    cout << "Number of Pistols: " << numberPistols << endl;
    cout << "Number of Knives: " << numberKnives << endl;
    cout << "Total of Weapons: " << numberPistols + numberKnives << endl;

    cout << "There's a new Pistol available!" << endl;

    numberPistols = numberPistols + 1;
    cout << "New amount of Pistols " << numberPistols << endl;
    cout << "Updated total of Weapons " << numberWeapons << endl;

    return 0;
}

谢谢和最诚挚的问候!

7 个答案:

答案 0 :(得分:1)

正如Carcigenicate已经评论的那样,通常的方法是写一个函数

#include <iostream>
using namespace std;

int getNumberWeapons(int numberPistols, int numberKnives)
{
    return numberPistols + numberKnives;
}

int main() {
    int numberPistols = 5;
    int numberKnives = 18;

    cout << "Number of Pistols: " << numberPistols << endl;
    cout << "Number of Knives: " << numberKnives << endl;
    cout << "Total of Weapons: " << getNumberWeapons(numberPistols, numberKnives) << endl;

    cout << "There's a new Pistol available!" << endl;

    numberPistols = numberPistols + 1;
    cout << "New amount of Pistols " << numberPistols << endl;
    cout << "Updated total of Weapons " << getNumberWeapons(numberPistols, numberKnives) << endl;

    return 0;
}

更好的解决方案是上课。这可能会被夸大,但如果你想扩展你的“武器卡宾”,这将是合适的。它添加了几个成员函数来操作类的状态并获取它们的访问权。

#include <iostream>
using namespace std;

class WeaponCabinet
{
public:
    void setNumberPistols(int pistols)
    {
        numberPistols = pistols;
    }

    void setNumberKnives(int knives)
    {
        numberKnives = knives;
    }

    void addPistol()
    {
        ++numberPistols;
    }

    void addKnive()
    {
        ++numberKnives;
    }

    void removePistol()
    {
        if (numberPistols == 0) return;
        --numberPistols;
    }

    void removeKnive()
    {
        if (numberKnives == 0) return;
        --numberKnives;
    }

    int getNumberKnives()
    {
        return numberKnives;
    }

    int getNumberPistols()
    {
        return numberPistols;
    }

    int getNumberWeapons()
    {
        return numberKnives + numberPistols;
    }

private:
    int numberPistols;
    int numberKnives;
};

int main() {
    WeaponCabinet weaponCabinet;
    weaponCabinet.setPistols(5);
    weaponCabinet.setKnives(18);

    cout << "Number of Pistols: " << weaponCabinet.getNumberPistols() << endl;
    cout << "Number of Knives: " << weaponCabinet.getNumberKnives() << endl;
    cout << "Total of Weapons: " << weaponCabinet.getNumberWeapons() << endl;

    cout << "There's a new Pistol available!" << endl;

    weaponCabinet.addPistol();
    cout << "New amount of Pistols " <<  weaponCabinet.getNumberPistols() << endl;
    cout << "Updated total of Weapons " << weaponCabinet.getNumberWeapons() << endl;

    return 0;
}

通过这种方式,您可以直接连接刀具和手枪,并且不必每次都将它们传递给函数getNumberWeapons

如果你不需要整个类处理你的武器,最现代的技术就是使用lambda(并且使用函数更接近解决方案)

#include <iostream>
using namespace std;

int main() {
    int numberPistols = 5;
    int numberKnives = 18;
    auto numberWeapons = [&numberPistols, &numberKnives]()
        {
            return numberPistols + numberKnives;
        };

    cout << "Number of Pistols: " << numberPistols << endl;
    cout << "Number of Knives: " << numberKnives << endl;
    cout << "Total of Weapons: " << numberWeapons() << endl;

    cout << "There's a new Pistol available!" << endl;

    numberPistols = numberPistols + 1;
    cout << "New amount of Pistols " << numberPistols << endl;
    cout << "Updated total of Weapons " << numberWeapons() << endl;

    return 0;
}

答案 1 :(得分:0)

这可能不是你想要在你的第一堂C ++课程中学到的东西,但是如果你正在使用C ++ 11,你可以用一个lambda函数替换int numberWeapons,这会增加您调用时numberPistolsnumberKnives的当前值:

  auto numberWeapons = [&numberPistols, &numberKnives]() {
    return numberPistols + numberKnives;
  };

numberWeapons现在是一个函数,可以从语句的numberPistol部分了解numberKnives[&numberPistols, &numberKnives]变量。它不需要参数 - () - 它返回两个变量的总和。

(事实上()可以在这里省略,因为它们是空的,这个函数没有参数,但是我把它们留了下来,因为它使它更容易辨认作为lambda函数,通常看起来像[…](…){…}。)

#include <iostream>
using namespace std;

int main() {
  int numberPistols = 5;
  int numberKnives = 18;
  auto numberWeapons = [&numberPistols, &numberKnives]() {
    return numberPistols + numberKnives;
  };

  cout << "Number of Pistols: " << numberPistols << endl;
  cout << "Number of Knives: " << numberKnives << endl;
  cout << "Total of Weapons: " << numberWeapons() << endl; // outputs 23
  //                         note the brackets ^^

  cout << "There's a new Pistol available!" << endl;

  numberPistols = numberPistols + 1;
  cout << "New amount of Pistols " << numberPistols << endl;
  cout << "Updated total of Weapons " << numberWeapons() << endl; // outputs 24
  //                                                  ^^

  return 0;
}

答案 2 :(得分:0)

写作时

int numberWeapons = numberPistols + numberKnives;

这不会创建一个&#34;等式&#34;每次更改后自动更新。这行在开头执行一次,因为没有循环,所以永远不会再执行。

您需要在更改后手动更新总计:

#include <iostream>
using namespace std;

int main() {
    int numberPistols = 5;
    int numberKnives = 18;
    int numberWeapons = numberPistols + numberKnives;

    cout << "Number of Pistols: " << numberPistols << endl;
    cout << "Number of Knives: " << numberKnives << endl;
    cout << "Total of Weapons: " << numberPistols + numberKnives << endl;

    cout << "There's a new Pistol available!" << endl;

    numberPistols = numberPistols + 1;
    //Manually update the variable. 
    numberWeapons = numberPistols + numberKnives;

    cout << "New amount of Pistols " << numberPistols << endl;
    cout << "Updated total of Weapons " << numberWeapons << endl;

    return 0;
}

请注意,除了int之外,它看起来就像它第一次设置一样,因为我不是声明 numberWeapons只是重新分配它。

真实&#34;程序,你使用一个处理更新的功能,但我认为这只会掩盖逻辑听力,因为你只是将2个数字加在一起。

答案 3 :(得分:0)

变量彼此独立。为了向您解释,我们可以从苹果开始:

  

我给露西两个苹果。我给你三个苹果并说'#34;我给你一个苹果加上我给露西的苹果数量。&#34;然后,过了一段时间,我又给露西送了一个苹果。你有几个苹果?

答案很简单:它是三个。就像C ++中的变量或任何其他编程语言一样,你和露西拥有的苹果数量是相互独立的。变量彼此独立。

变量值的变化不会影响另一个变量的值,即使您使用变量计算另一个变量的值也是如此。

如果您希望始终拥有更新的号码,该怎么办?

然后您必须每次计算值。程序员通常使用函数执行此操作。在您的示例中,我们可以将武器总数定义为返回numberPistolsnumberKnives的函数。以下是我的表现:

#include <iostream>
using namespace std;

int main() {
    int numberPistols = 5;
    int numberKnives = 18;
    auto computeNumberWeapons = [&]{ return numberPistols + numberKnives; };

    cout << "Number of Pistols: " << numberPistols << endl;
    cout << "Number of Knives: " << numberKnives << endl;
    cout << "Total of Weapons: " << numberPistols + numberKnives << endl;

    cout << "There's a new Pistol available!" << endl;
    numberPistols = numberPistols + 1;

    cout << "New amount of Pistols " << numberPistols << endl;

    cout << "Updated total of Weapons " << computeNumberWeapons() << endl;
    return 0;
}

现在,computeNumberWeapons不是int类型的变量,而是一个计算前两个变量之和的函数。您必须使用()运算符调用该函数。

答案 4 :(得分:0)

在这个程序中,所有语句都是按顺序执行的,每个语句只执行一次。 您必须重写或循环语句以重复。

int numberPistols = 5;
int numberKnives = 18;
int numberWeapons = numberPistols + numberKnives;

让我们对您的计划进行干预:

  • 首先,您已声明整数类型变量numberPistols并使用值“5”初始化它。
  • 然后,您已声明整数类型变量numberKnives并使用值“18”对其进行初始化。
  • 在下一个语句中,您已声明变量numberWeapons并使用从numberPistolsnumberKnives之和获得的值对其进行初始化。
  • 打印完所有变量后,您将numberPistols的值增加1。

错误

您已更新numberPistols但尚未更新numberWeapons。更改numberPistolsnumberKnives的值不会影响numberWeapons的值,因为它已经存储,您必须使用新值更新存储的值以进行更改。

解决方案

添加此行numberWeapons = numberPistols + numberKnives;

numberPistols = numberPistols + 1;

之后

现在,您的最终代码将是:

#include <iostream>
using namespace std;

int main() {
    int numberPistols = 5;
    int numberKnives = 18;
    int numberWeapons = numberPistols + numberKnives;

    cout << "Number of Pistols: " << numberPistols << endl;
    cout << "Number of Knives: " << numberKnives << endl;
    cout << "Total of Weapons: " << numberPistols + numberKnives << endl;

    cout << "There's a new Pistol available!" << endl;

    numberPistols = numberPistols + 1;
    numberWeapons = numberPistols + numberKnives;         //update the numberWeapons here

    cout << "New amount of Pistols " << numberPistols << endl;
    cout << "Updated total of Weapons " << numberWeapons << endl;

    return 0;
}

答案 5 :(得分:0)

我理解并尝试了一些人们在这里发布的不同代码,其中一个代码在我尝试编译和运行时给出了错误(下面是代码)。我使用的是Dev-C ++ 5.11。

int main() {

int numberPistols = 5;
int numberKnives = 18;
auto computeNumberWeapons = [&]{ return numberPistols + numberKnives; };

cout << "Number of Pistols: " << numberPistols << endl;
cout << "Number of Knives: " << numberKnives << endl;   
cout << "Total of Weapons: " << numberPistols + numberKnives << endl;   
cout << "There's a new Pistol available!" << endl;
numberPistols = numberPistols + 1;
cout << "New amount of Pistols " << numberPistols << endl;
cout << "Updated total of Weapons " << computeNumberWeapons()) << endl;
return 0;
}

有时候我使用的这个IDE +编译器会让我想一想,如果代码错误或者我在安装并在其中启动项目时做错了什么......

Error Image

答案 6 :(得分:0)

好吧,如下所示,在int main()

之前添加此函数
//function for calculating numberWeapons 
int totalWeapons(int numberPistols, int numberKnives){ 
     return numberPistols+numberKnives; 
}

并在下面的地方添加您想要更新的numberWeapons:

numberWeapons = totalWeapons(numberPistols, numberKnives);

即。你的代码就像:

#include <iostream>
using namespace std;

//function for calculating numberWeapons 
int totalWeapons(int numberPistols, int numberKnives){ 
     return numberPistols+numberKnives; 
}

int main() {
    int numberPistols = 5;
    int numberKnives = 18;
    int numberWeapons = totalWeapons(numberPistols, numberKnives); //call function here

    cout << "Number of Pistols: " << numberPistols << endl;
    cout << "Number of Knives: " << numberKnives << endl;
    cout << "Total of Weapons: " << numberWeapons << endl;

    cout << "There's a new Pistol available!" << endl;

    numberPistols = numberPistols + 1;

    //call function here to get updated numberWeapons
    numberWeapons = totalWeapons(numberPistols, numberKnives); 

    cout << "New amount of Pistols " << numberPistols << endl;
    cout << "Updated total of Weapons " << numberWeapons << endl;

    return 0;
}