+ 1
[Practice 6.2] Why isnāt this code accepted/working?
Hello, Iām a newbie who just started coding a few weeks ago :) Iām curious on why this code doesnāt swap the numbers around. Thanks, in advance! #include <iostream> using namespace std; int main() { int aquariumDavid = 8; int aquariumAlex = 11; int aquariumthree = 8 + 11; // your code goes here { int aquariumDavid = aquariumthree - 11; } { int aquariumAlex = aquariumthree - 8; } cout << "David's aquarium: " << aquariumDavid << endl; cout << "Alex's aquarium: " << aquariumAlex; return 0; } Edit: I eventually got pass the practice with a different code, Iām just curious on why this doesnāt work :)
2 Answers
+ 4
Because aquariumDavid/Alex is not the aquariumDavid/Alex you think
What you're actually doing is:
int aD = 8;
int aA = 11;
int t = 8 + 11;
{
int aD1 = t-11;
}
{
int aA1 = t-8;
}
Also, aD1 and aA1 are lost once you exit the {}
To solve this just delete the int before those statements, so you don't create new variables
+ 2
Yep, it works now! Iāve just realized that Iāve completely messed up basic math too, haha