+ 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 odpowiedzi
+ 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