Search code examples
c++variables

Result of calculation is not changed when one of the variables is updated later


In the following code everything went fine except the last "total of weapons" that was not updated when I added +1 to the numberPistols. I understood that the numberPistols was updated but when using the numberWeapons on the last line it don't show the new value.

I still cannot understand why, can somebody explain me and show me what I'm doing wrong?

#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;
}

Solution

  • In this program, all the statements are being executed sequentially and every statement executes only once. You have to rewrite or loop the statement to repeat.

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

    Let's do a dry run on your program:

    • First of all, you have declared an integer type variable numberPistols and initialized it with value '5'.
    • then, you have declared an integer type variable numberKnives and initialized it with value '18'.
    • In next statement, you have declared a variable numberWeapons and initialized it with value obtained from the sum of numberPistols and numberKnives.
    • After printing all variables, you're incrementing the value of numberPistols by 1.

    Mistake:

    You have updated numberPistols but you have not updated numberWeapons again. Changing the value of numberPistols or numberKnives will not affect the value of numberWeapons because it is stored already and you have to update the stored value with new value to make the change happened.

    Solution:

    Add this line numberWeapons = numberPistols + numberKnives;

    after numberPistols = numberPistols + 1;

    Now, your final code will be:

    #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;
    }