Function that appends a string to another string a number of times.

I am trying to have string appended to string a number of times using the return function but it isn't working. Here is my code:

string appendTimes(string firstString, string secondString, int integer)
{
for(int i=0; i< integer; i++)
{
return firstString + secondString;
}
}

It should look like, appendTimes( "hello", "there", 2) == "hellotherethre"
but the output is, "hellothere".

We are not supposed to use cout, only return and I have not learned how to use the append function yet, only loops.

Thank you!
1
2
3
4
5
6
7
8
string appendTimes(string firstString, string secondString, int integer)
{
  for(int i=0; i< integer; i++)
  {
     firstString += secondString;
  }
  return firstString;
}
Do you realize that when the program encounters a return statement it returns immediately to the calling function anything after the return is not executed?


Topic archived. No new replies allowed.