I don’t quite understand the problem here. The codecademy platform compiles and executes the code by itself, so I wanted to check it out in the real terminal and stumbled across an error. The part of the code I have issues is: std::vector even = {2, 4, 6, 8, 10};
the terminal identifies this line as an error and says this:
non-aggregate type ‘std::vector’ cannot be initialized with an initializer list
You can try to add the vector datatype, like std::vector<“datatype”> name = {values};
But then you will probably encounter another error. Depends on your compiler.
“[Error] could not convert ‘{2, 4, 3, 6, 1, 9}’ from ‘’ to ‘std::vector’”
I got this and found out it was from my compiler because is a bug that has been fixed and my compiler is outdated. So, try this out.
Also, can you share both your code and the error message?
(There is a “Copy to Clipboard” button at the bottom of the exercises. To post the code in the forums, you can use the </> button (see: [How to] Format code in posts)
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {2, 4, 3, 6, 1, 9};
int even=0;
int uneven=0;
for (int i = 0; i < numbers.size(); i++) {
if (numbers[i]%2 == 0) {
even = even + numbers[i];
}
else {
uneven = uneven + numbers[i];
}
}
std::cout << "Even numbers total: " << even << "\n";
std::cout << "Uneven numbers total: " << uneven << "\n";
}
The Code works fine in the Codecademy environment, but kept crashing in my VSCode. So I was googling for a while and came across a solution on stackoverflow to include -std=c++17.
Screenshot from VSCode Terminal (same code as above):
So after 2h of googling around, I sort of understood that the compiler version that my computer uses is below C++11 and that is why it can’t read the initializer list.
I can’t figure out how to update to a newer compiler version (Macbook Pro 2014 - Big Sur).
Does that make sense to you and do you have a solution?
I am not familiar with your environment, but this post looks useful (if you do decide to edit your tasks.json file, make sure to copy the contents of the file before making any changes so that if things don’t go well, you can at least reverse the changes):