1. ## String (?) Recursion

Hey all,
I have an assignment for my beginning C++ class where my teacher wants us to use recursion to display ten stars ( ********** ) n times vertically. such that if n = 4 it would look like:

**********
**********
**********
**********

now normally I would use a void function that just does:

Code:
```void rectangle(int n){
if (n >= 1) cout << "**********";
rectangle (n-1);
}```
but my teacher would like us to cout the return of ten stars. How is that supposed to look?

I tried:

Code:
```#include <iostream>
using namespace std;

string rectangle(int n){
if (n >= 1) return "**********";
rectangle(n-1);
}

int main(){
int n;
cout << "Please enter an n value: ";
cin >> n;
cout << rectangle(n);
return 0;
}```
but it doesnt seem to be working.

Thanks for any and all help.

2. I don't see where you are using the return value of the call to rectangle inside rectangle. Perhaps you should be adding them all together?

3. Code:
```void rectangle(int n){
if (n >= 1) cout << "**********";
rectangle (n-1);
}```
That's an infinitely recursive function. You only want to recurse while n is greater than 0, currently you always recurse so the call to rectangle(0) will not print the stars but it will call rectangle(-1)... which calls rectangle(-2)... which calls rectangle(-3)... etc... Eventually it reaches the point where it calls rectangle(-2147483648) (assuming 4 byte ints and assuming you haven't blown the stack at that point causing the program to crash) and then switches from a negative to a positive value and you'll start printing 2+ billion lines of stars and the cycle just keeps repeating.

A quick fix is to add in some braces to help control when you'll recurse.
Code:
```void rectangle(int n){
if (n >= 1)
{
cout << "**********\n";
rectangle (n-1);
}
}```
Just wanted to make sure some newb that thought to try this out would not wonder what's going wrong. This doesn't really address the question you're asking.[/edit]

4. Originally Posted by hk_mp5kpdw
Code:
```void rectangle(int n){
if (n >= 1) cout << "**********";
rectangle (n-1);
}```
That's an infinitely recursive function. You only want to recurse while n is greater than 0, currently you always recurse so the call to rectangle(0) will not print the stars but it will call rectangle(-1)... which calls rectangle(-2)... which calls rectangle(-3)... etc... Eventually it reaches the point where it calls rectangle(-2147483648) (assuming 4 byte ints and assuming you haven't blown the stack at that point causing the program to crash) and then switches from a negative to a positive value and you'll start printing 2+ billion lines of stars and the cycle just keeps repeating.

A quick fix is to add in some braces to help control when you'll recurse.
Code:
```void rectangle(int n){
if (n >= 1)
{
cout << "**********\n";
rectangle (n-1);
}
}```
Just wanted to make sure some newb that thought to try this out would not wonder what's going wrong. This doesn't really address the question you're asking.[/edit]
Code:
```void rectangle(int n){
if (n >= 1)
{
cout << "**********\n";
}
rectangle (n-1);

}```
I was doing that.

5. I think what was pointed out is that you should try and take a look at the logic of your if-statement and the placement of the call to rectangle within the rectangle function. And see why you will get infinite recursion... Because you're not doing the same in your code as hk_mp5kpdw showed in his code.

6. There's no infinite recursion in the current attempt (the one with infinite recursion is the earlier attempt).

One problem is that the base case is wrong - it calls the function recursively and the rest of the cases return. It should be the other way around, the function should recurse except in the base case.

Also each string being returned is ignored except the last one.