FAQ: Code Challenge: C++ Functions - First Three Multiples

The body of the for loop runs like this:
std::cout << element << “\n”;
Here “\n” is an escape sequence (it basically helps to format the output on the monitor)
which makes the control go to the next line. When the loop runs again, the output for that iteration will appear on the next line. Hope this helps :slightly_smiling_face:

1 Like

Did the same thing until i noticed that I used () instead of the needed {} to enclose the num, num2 num3 in the vector definition.

2 Likes

9 Likes

I have same issue…output is also produce correclty

1 Like

I think your problem is that you totally removed the main() function. The program was expecting you to have two functions, and call the first_three_multiples() in the main() function

1 Like

@citrus7 it have main function which is hide behind prompted message.(i.e Is first_three_multiples()).

Please refer following link to know what is the main reason to cause this problem.
link: Error in First Three Multiples

1 Like

according to suggestion given by codecademy moderator…it produce result without an error.


o/p: 8,16,24 without any error message

4 Likes

What does the for-loop do in this program? the syntax seems wrong compared to previous lessons. no semicolons and only 2 parts? the challenge doesnt explain it at all. how does the value of “first_three_multiples(8)” get put in “element”?

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

for (int element : first_three_multiples(8))

it’s called range based for loop.

I am not having much knowledge about this,
but basically iterate vector from i=0 to i<vector.size()
and each time of iteration it assign new value to the variable(‘element’).

when i=0 => element=8;
i=1 => element=16;
i=2 => element=24;
for better understanding of range based for loop read following article
link:- https://www.geeksforgeeks.org/range-based-loop-c/
(also you can check on IDE which can give better idea how range based loop works)

10 Likes

Hello,
When I run my code, it first returns 0 0 0 and then the correct answer 8 16 24, but I don’t get why. Any suggestions?

Edit:
When I changed my code to this, the 0 0 0 disappeared. But why? :smiley:

(left first code, right edited code)

In your first attempt, you specified a size for the vector. You did not include an initial value, so 0 was assigned. If you were to edit that line of code, and provide an initial value like so:

std::vector<int> multiple(3, 11):

You would have a vector that looks like this: {11, 11, 11}. Then you append the first three multiples of 8 to that vector, so your output would be:

11
11
11
8
16
24

When you removed (3), you instantiated an empty vector {} rather than a vector with 3 elements assigned the value of 0 by default {0, 0, 0}.

6 Likes

Code will not pass but i have it working on the console i have an output of 8, 16, 24. but its not passing! I have checked several times and im sure of my code, but even without looking at the code i clearly have passed as the requested output is working!

"Both includes iostream and vector added.

// Define first_three_multiples() here:
std::vector first_three_multiples (int num) {

std::vector multiples {num, num * 2, num * 3};

return multiples;
}

int main() {

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

};

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!

1 Like

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){}

2 Likes

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.

2 Likes


Can anyone tell me what is the problem here?
Edit -> Sorry Got it.It’s space problem as above.

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.

2 Likes

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";
  }
  
}