Calling a function from a loop

So I`m trying to solve a simple task by myself but I`ve only been coding for a week. I`ve looked at everyone elses questions to see if they could help me but my brain is not clicking. I`m trying to call my pretty function 6 times within my main function if someone could show me how that would be great.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

void pretty( ){
    int x = 0;
    x++;
    for(int i = 0; i < x; ++i){cout<<'*';}
    cout<<endl;
}
int main(){
    int i = 0;
    while(i<=6){
        cout<<i++;
    }
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

void pretty( ){
    int x = 0;
    x++;
    for(int i = 0; i < x; ++i){cout<<'*';}
    cout<<endl;
}
int main(){
    int i = 0;
    while(i<=6){
        cout<<i++;
    }
    cout << endl;
    for(i = 0; i < 6; i++) {
        pretty();
    }
    return 0;
}
But I don't think this gives you the results you were hoping for.

I believe what you want is called recursion. Like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

void pretty(int x ){
    //int x = 0;
    x++;
    for(int i = 0; i < x; ++i){cout<<'*';}
    cout<<endl;
    if(x < 7) {
        pretty(x);
    }
}
int main(){
    int i = 0;
    while(i<=6){
        cout<<i++;
    }
    cout << endl;
    pretty(0);
    return 0;
}
I was given the first part by my teacher. The step was to write a main with a loop that calls the function six times. I think he wants us to just tell him what is the output.
Topic archived. No new replies allowed.