Hi! Had the same issue as well. Defining vectors is a tricky business. It is as follows std::vector then type enclosed in <> and vname .
Good luck!
Hey, if you are wondering why it’s not letting you pass the lesson even if the output is correct, check to see if you have done these things:

If you use ‘using namespace std;’ at the top of your program, make sure you add the std:: to the start of your vector definition, so that it looks like std::vector first_three_multiples(int num){}. This is unnecessary for the code to run properly but will stop you from clearing the course.

Make sure you add a space before the function name in the function definition.
Like this: std::vector first_three_multiples(int num){}
NOT like this: std::vectorfirst_three_multiples(int num){}
Actually, after spending some time trying to diagnose why my answer wasn’t being accepted it turned out that it was because I had written std::vector<int> first_three_multiples (int num){
instead of std::vector<int> first_three_multiples(int num){
.
So if anyone already has the space and it isn’t letting you pass, try removing it.
this makes me nervous…it did this to me for like a thousand times …and the solution is the same …literally
As @jppappas.jp noticed about @code6998751430 's code, the multiples vector should be initialized using curly braces {}
instead of parentheses ()
. That is why the compiler is throwing errors. If you post your code, perhaps someone will point out a mistake.
Burh, this is the shortest and cleanest code i’ve seen so far. Easy read.
This is what I did:
#include <iostream>
#include <vector>
// Define first_three_multiples() here:
std::vector<int> first_three_multiples(int num) {
return std::vector<int>{num, num * 2, num * 3};
}
int main() {
for (int element : first_three_multiples(12)) {
std::cout << element << "\n";
}
}
std::vector<int> multiples{num, num * 2, num *3};
For this line of code why are {} acceptable but not ()?
multiples
is a variable of type vector
comprising of integer elements. We can initialize a vector by using curly braces {}
.
We can use parentheses ()
when initializing a vector, but only in specific ways.
For example:
std::vector<int> multiples(3)
would create an integer vector called multiples
of size 3 (with all 3 elements taking on the default value of 0).
std::vector<int> multiples(3, 20)
would create an integer vector called multiples
of size 3 (with all 3 elements being assigned the value 20).
There are other uses such as using ranges or copying another vector’s elements. These would also involve the use of parentheses. See this link for examples on how parentheses are being used while creating vectors: http://www.cplusplus.com/reference/vector/vector/vector/
std::vector<int> multiples{num, num * 2, num *3}
will work.
std::vector<int> multiples(num, num * 2, num *3)
won’t work because it doesn’t fall into one of the valid uses of parentheses (see examples and link above) while declaring & initializing a vector.
Here is a screenshot from the documentation where parentheses have been used in suitable manner (Source of screenshot: https://docs.microsoft.com/enus/cpp/standardlibrary/vectorclass?view=vs2019#example39).
Here is my code, worked
#include
#include
// Define first_three_multiples() here:
std::vector first_three_multiples(int num) {
std::vector vector;
for(int i=0;i<3;i++){
vector.push_back(num*(i+1));
}
return vector;
}
int main() {
for (int element : first_three_multiples(8)) {
std::cout << element << “\n”;
}
}
Can someone tell me why my function below isn’t working for this task? Thanks in advance
std::vector first_three_multiples(int num) {
std::vector temp(3);
for (int i = 1; i <= temp.size(); i++) {
temp.push_back(num * i);
}
return temp;
}
Here are some issues with your function:

The return type of the function should be
std::vector<int>
instead ofstd::vector

In the statement
std::vector temp(3)
, again you should replacestd::vector
withstd::vector<int>

When you made the declaration
std::vector<int> temp(3)
, you actually initialized a vector of integers with 3 elements with all the elements taking on the default value of 0. Then, inside your for loop, you keep pushing elements onto the vector. You aren’t replacing the first three elements, you are just tacking on new elements to the end of the vector. Since your loop condition is looking at the size of the vector, so you end up with an infinite loop where you just keep pushing back multiples to the vector and the size keeps growing. There are many ways you can avoid this. You could declare your vector asstd::vector<int> temp
. This would create an empty vector. If you decide to go this route, you will also have to edit your loop condition. Instead of looking at the size of the vector, you could edit the condition toi <= 3
. If you don’t want to declare an empty vector but wish to initialize it asstd::vector<int> temp(3)
, then you will have to modify your code so that you are replacing the first three elements instead of pushing back new elements.
so, i did things a little differently. but the program functions as instructed? yet the website still marks it as incorrect
Why have you changed the code in main? Perhaps that is causing the issue. The example in the instructions is only meant to show us the expected output. But, we aren’t supposed to change the code in main.
This solution seems to work.
The test only passes if you call the function with an argument of 8. Using 7 from the example fails the test. I reported the bug so hopefully it gets fixed, but this will help you pass it for now.
This might help, as I too was stuck for a bit. You just need to remember to go back to the basics of what we have learned thus far.
 We need a function that returns a vector.
 The elements in the vector are int’s.
 The functions parameter is an int.
So to return a vector, we must declare the function as a vector type with std::vector. Then we must specify that the elements in the vector, in our case, are int’s with < int >.
So step 1 and 2 look like std::vector< int >.
Finally we need to set the parameters of the function, in our case the parameter is an int which looks like first_three_multiples(int num).
Step 1, 2, and 3 look like this std::vector< int > first_three_multiples(int num).
But we are not done yet…
Don’t forget we need to build a vector in the function, so our function can return a vector.
In the main() we are passing an int into our function when we call the function. When we pass the int to our function, we then used that number to .push_back into the vector we created in the function. We then had the function return the vector created in the function with all of our .push_back elements in the vector.
Look at the code below to see it all in action!
Hope this helps!
#include < iostream >
#include < vector >
std::vector< int > first_three_multiples(int num)
{
std::vector< int > numbers{};
numbers.push_back(num);
numbers.push_back(num2);
numbers.push_back(num3);
return numbers;
}
int main()
{
for (int element : first_three_multiples(3))
{
std::cout << element << “\n”;
}
}
in your solution, where did you get the function “multiples”??
std::vector multiples{num, num * 2, num * 3};
and if in your answer you are spewing the first three multiples already, what’s the forloop for in the mains??