C++ function multiple

hello im this topic:
https://www.codecademy.com/courses/learn-c-plus-plus/lessons/cpp-code-challenge-functions/exercises/cpp-functions-cc-first-three-multiples

and have this done: i now is bad but not remember how start a multiple function


#include <iostream>
#include <vector>
int first_three_multiples();

// Define first_three_multiples() here:
int first_three_multiples(int num){
	int n=8;
	for(i=0;i < n; i++){
    if(n%2=0){
      return = std:cout<<n;<<endl;
    }
  }
}

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

1 Like

First, If you could re-post your code, so that it is formatted as code, that will help others to read it more easily. To do so, you can type 3 back tics on the line above your code, and on the line below your code like so:

```
Paste your code here.
```
1 Like

thanks i edit the post

1 Like

Ok. The function needs to return a vector, so to define your function, the first thing you have to do is declare the data type that it will return which in this case is a vector containing integers. Like so:

std::vector<int> first_three_multiples(int num) {
  //function code here
}

The body of your current function won’t accomplish the task. The pre-written code provided will test your function by passing in the value: 8. You won’t need to assign 8 to a new variable, you just use the num parameter included in your function declaration. What you need to do is multiply num by 1 then 2 then 3 to get the first 3 multiples: 8, 16, 24. Those values should be stored in a vector of type int. Then return the vector. To declare an empty vector of type int you use the following syntax: std::vector<int> myVector;
You can then add values to the vector with the .push_back() method or you can assign the values when you declare the vector like so: std::vector<int> myVector = {value1, value2, value3}; Give it a try. Good luck!

1 Like

i get this , what i dont understand where go : std::vector myVector;

#include <iostream>
#include <vector>

// Define first_three_multiples() here:
std::vector<int> first_three_multiples(int num) {
  
//std::vector<int> multiples{num, num * 2, num * 3};
  // este es el vector declarado
  int vector [] ={num, num * 2, num * 3};
return vector;
}

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

Un-comment the above line, and delete the following:

Then change return vector; to return multiples;, and I believe you’ll have it :slightly_smiling_face:

1 Like

Sorry, I missed one thing:

You need an = between multiples and {}

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

Could you explain me how the ‘for loop’ (a small part of the question) works? (As far as I remember this is not taught till this part of the course.)

for loops in general in all programming languages are most often used to iterate over an enumerable data structure like an array or in the case of this lesson a vector. The syntax used in this lesson is a little more advanced than usual. In the example, int element is a variable that will contain the values returned by the range on the right side of the :. The range in this case is a function called first_three_multiples. The function returns a vector, and the for loop assigns each element or value included in the vector to the variable named element one at a time. It takes the first value from the vector, performs whatever actions are inside the for loop’s code block, then repeats those actions for the second value, third value, etc. until it reaches the end of the range. In this example there are only 3 values in the vector, and the only action inside the code block is to print the value to the console using std::cout. Hopefully this makes sense. Here’s a link regarding the use of the : in the loop if you’d like to investigate further: https://www.cprogramming.com/c++11/c++11-ranged-for-loop.html

3 Likes

Thank you for this! This was definitely not in any of the lessons so far, but you explained it perfectly! Well, for me at least!

1 Like

They definitely go through for loops early on in the lessons but I to was confused by the single “:” which they do not cover. So I appreciate this thread.