Reputation: 33
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;
}
Upvotes: 3
Views: 4319
Reputation: 106
Alright make a function like below add this function before int main()
//function for calculating numberWeapons
int totalWeapons(int numberPistols, int numberKnives){
return numberPistols+numberKnives;
}
and add the line given below wherever you want updated numberWeapons:
numberWeapons = totalWeapons(numberPistols, numberKnives);
i.e. your code will be like:
#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;
}
Upvotes: 0
Reputation: 33
I understood and also tried a different codes that you people posted here, one of them is giving me error when I try to compile and run (below is the code). I'm using 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;
}
Sometimes this IDE + Compiler that I use makes me think if it's the code that's wrong or something that I did wrong when installed and started a project inside it...
Upvotes: 0
Reputation: 3357
This is maybe not something you'd want to learn in your first C++ lesson, but if you're using C++11 you could replace int numberWeapons
with a lambda function which adds up the current values of numberPistols
and numberKnives
at the time you call it:
auto numberWeapons = [&numberPistols, &numberKnives]() {
return numberPistols + numberKnives;
};
numberWeapons
is now a function which knows about the numberPistol
and numberKnives
variables, from the [&numberPistols, &numberKnives]
part of the statement. It takes no arguments - ()
- and it returns the sum of the two variables.
(In fact the ()
can be omitted here as they're empty, there's no arguments for this function, but I've left them in because it makes it more recognisable as a lambda function, which generally look like […](…){…}
.)
#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;
}
Upvotes: 0
Reputation: 106
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:
numberPistols
and initialized it with value '5'.numberKnives
and initialized it with value '18'. numberWeapons
and initialized it with value obtained from the sum of numberPistols
and numberKnives
.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;
}
Upvotes: 1
Reputation: 610
As Carcigenicate already comments, one usual way would be to write a function
#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;
}
A even better solution would be to make a class. This might be exaggerated, but would be appropriate if you want to extend your "weapon carbinet". It adds several member functions to manipulate the state of the class and get access to them.
#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;
}
In this way you have a direct connection between the Knives and Pistols and have not to pass them every time to the function getNumberWeapons
.
If you do not need an entire class handling your weapons the most modern technique would be to use a lambda (and it is much closer to the solution using a function)
#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;
}
Upvotes: 1
Reputation: 41750
Variable are independent each other. To explain this to you, we could start with apples :
I give two apples to Lucie. I give you three apples and say "I give you one apple plus the number of apples I gave to Lucie." Then, some time later, I give another apple to Lucie. How many apples do you have?
The answer is quite simple : it's three. Just like variables in C++, or any other programming language, the number of apples you and Lucie have are independent of each other. Variables are independent of each others.
Mutating the value of a variable won't affect the value of another one, even if you use a variable to compute the value of the other one.
What if you'd like to always have an updated number?
Then you'd have to compute the value each time. Programmers usually do this with a function. In your example, we could define the total number of weapon as a function that returns numberPistols
plus numberKnives
. Here's how I'd do it :
#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;
}
Now, computeNumberWeapons
is not a variable of type int
, but a function that compute the sum of the two previous variables. You have to call that function with the ()
operator.
Upvotes: 0
Reputation: 45736
When you write
int numberWeapons = numberPistols + numberKnives;
This doesn't create an "equation" that auto updates itself after each change. This line is executed once at the beginning, and since you have no loops, is never executed again.
You need to manually update the total after a change:
#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;
}
Notice it looks just like the first time it's set, except for the int
, since I'm not declaring numberWeapons
just re-assigning it.
In "real" programs, you'd use a function that handles the updating, but I think that would only obscure the logic hear since you're just adding 2 numbers together.
Upvotes: 0