CODE CHALLENGE: C++ FUNCTIONS First Three Multiples

I don’t know how to set my function data type as a vector?

#include <iostream>
#include <vector>

// Define first_three_multiples() here:
 int first_three_multiples(int num) {
  return std::vector = (num, num * 2, num * 3);
}

int main() {
  
  for (int element : first_three_multiples(8)) {
    std::cout << element << "\n";
  }
  
}

That’s my code and I don’t know what’s wrong with it.

The type would be std::vector<int>
I suggest googling for examples on how to create vectors.
Also, are you returning or assigning on that line? And is that a variable name or a type? … Yeah, google basic operations for vector.

1 Like

Thanks! I put in the data type, put the vector on its own line, gave it a name, and returned the name of the vector.

You seem to be trying to right c++ as you would a more dynamic language.
Your vector construction is a bit off.

Heres an example program in c++14
Showing a more “C++ like” way of doing things

#include <iostream>
#include <vector>

int main()
{
        int x{3};
        std::vector<int> multiples{ {x},{x*2}, {x*3}};
        for(const auto& multiple : multiples)
                std::cout << multiple << "\n";


        return 0;
}

Note the initialization of the int and vector
Try converting this code into it’s own function like you were doing

1 Like