FAQ: Functions - Tackling Multiple Arguments

I know about that. I don’t want “rand om 112!” and then next line but as it is right now. Problem as I see is that about first 1000 lines writes ok then suddenly shifts.

its so simple the std::string x is part of #include while int x is simple iostream member so you don’t have to write std::string x

Stuck on step 4.
I’ve done what I think it wants in the int main

int main() {

std::string my_name = “Thomas”;

int some_number = 5; // Change this if you like!

// Call name_x_times() below with my_name and some_number

name_x_times (my_name, some_number);

}

However, this is still saying I’ve done it wrong could someone tell me what’ve I done wrong?

I’ve done what you did but it still saying I’ve done it wrong?

Hi guys.
So I’ve no idea why this is wrong.
I checked previous exercises and it seems to be correct compared to previous tasks.

Add “” around Gilles.

1 Like

I oversaw the template where I had to replace “add your name here!” with my name, then I get to the same code as thomasswift, above this post, but also same problem:
./main.cpp: In function ‘void name_x_times(std::__cxx11::string, int)’:
./main.cpp:8:11: error: expected primary-expression before ‘int’
while(int x > 0){
^~~
./main.cpp:8:11: error: expected ‘)’ before ‘int’
./main.cpp:8:17: error: expected initializer before ‘>’ token
while(int x > 0){

1 Like

Paste your code here. Easier to figure it out in an IDE.

No idea what’s done differently but I resetted the task and it worked now xD
Thanks a lot for the very fast response!

#include <iostream>

// Define name_x_times() below:
void name_x_times(std::string name, int x){
    while(x > 0){
        std::cout << name;
        x--;
    }
}

int main() {
  
  std::string my_name = "Gilles!";
  int some_number = 5; // Change this if you like!
  // Call name_x_times() below with my_name and some_number
  name_x_times(my_name, some_number);
  
}
2 Likes

change the x- into x–

I can’t get past the first step and don’t know why. The first step is to:

1.Define a void function name_x_times() that takes two parameters:

  • a string name
  • an integer x

Codeacademy keeps telling me that when I type my:
void name_x_times(std::string name, int x) code, it asks the question:

Is name_x_times defined as a void function with two arguments?
When I click view solution and even copy the code, reset the exercise, and paste it the the code seems to run, but codeadademy doesn’t give me the checkmark to indicate completion. Here’s my code:
#include

// Define name_x_times() below:
void name_x_times(std::string name, int x);

int main() {

std::string my_name = “Add your name here!”;

int some_number = 5; // Change this if you like!

// Call name_x_times() below with my_name and some_number

}

For x, I think you should use x-- . This is because it will cause the counter to decrease by 1 in a while loop. I am sorry if I am replying to a 1 year old comment.

#include

// Define name_x_times() below:

void name_x_times(std::string name, int x){

while(x>0){

std::cout << name;

x--;

}

}

int main() {

std::string my_name = “Sayhan !”;

int some_number = 5; // Change this if you like!

// Call name_x_times() below with my_name and some_number

std::cout << name_x_times(my_name,some_number);

}

Hello David, I think for the function void name_x_times(std::string name, int x), you need the curly braces for it to work in step 1. Do not put any semicolons on the function.

Thanks for the reply man, I don’t remember the question though, anyways thanks for the time you took out to clear it up.

1 Like

id name_x_times(std::string name, int x){

while(x > 0){

    std::cout << name;

    x--;

}

}

int main() {

std::string my_name = “Bob!”;

int some_number = 5; // Change this if you like!

// Call name_x_times() below with my_name and some_number

name_x_times(“Bob!”, some_number);

}
unknown y doesn’t work

Your code isn’t formatted as code, and it looks like you cut off the first line. When I run your code with the first line complete, and add the #include <iostream> at the top, it works for me:

#include <iostream>

void name_x_times(std::string name, int x){

    while(x > 0){

        std::cout << name;

        x--;

    }

}

int main() {

  

  std::string my_name = "Bob!";

  int some_number = 5; // Change this if you like!

  // Call name_x_times() below with my_name and some_number

  name_x_times("Bob!", some_number);

  

}

Output:

Bob!Bob!Bob!Bob!Bob!

See this for instructions on formatting your code.

Thanks for taking the time to clear that up!

Why does the first code snippet name the third parameter as ‘total_included’ whereas later in the text it seems to be referred to as ‘return_total’?

I’m simply wondering why the parameter order. It says it is important to call them in a specified order but also doesn’t explain the reasoning behind that order. If it was in order of appearance “bool total_included” would be the first argument, but instead it’s recommended last, or at least not middle.

If it’s from most “internal” to external, that could explain the price/tip first since they are “within” the if-statement, but that is not explained to properly write something solo outside this exercise without potential for error.

Just a question, why do you use “while (x>0)” and “x–”?
Please explain it to me