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

This community-built FAQ covers the “First Three Multiples” exercise from the lesson “Code Challenge: C++ Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise First Three Multiples

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I didn’t quite understand the for loop in the main() program. Could someone please explain what exactly does it do? I’d be grateful.

3 Likes

It’s like the for each function in other languages: for each value/element in the vector/array/list print out the multiples of each number. So here you’ve got the multiples of 8 in a vector IE {8,16,24}: print out each multiple(the element) on a new line.

3 Likes

Hi, I don’t really understand the use of the for loop in this exercise.

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

I thought a for loop, runs a block of code a certain number of times.
in this case, can’t we just call in the function ?

Edit: sorry I didn’t see the reply above. So follow up question, how come the loop prints out each multiple individually and not all of them on the same line?

2 Likes

If first_three_multiples is a function, then why do I have to put std::vector in front of it? Also, why is std::vector mentioned again inside the function? It seems redundant to me. Thank you.

image

7 Likes

yeah my codes the same as the solution, still doesnt work. this happens on so many of these lessons. how are you guys going to charge for a service that doesnt work correctly?

3 Likes


literally the same as the solution but counting it as wrong

Thanks a lot! That was clear and helped me get the meaning of the code :slight_smile:

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:

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

1 Like

2 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

@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

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


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

1 Like

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

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)

4 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}.

1 Like

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

};