I need help with ouputting stars

Hello I am trying to constuct a program without a main function that outputs stars but at the same time outputs the number and a colon after the number but before the number of starr.

This is the coding i have so far.

void output_stars(int num)
{
if (num > 0)
{
cout << "*";
output_stars(num-1)
}
else
{
cout << endl;
}
}

The program is working the way it should i just can't figure out how to output the number and a colon.

The main function is written as such:

output_stars(1);
output_stars(2);
output_stars(5);
output_stars(3);
output_stars(4);
output_stars(7);

all i need to figure out is how to get the numbers of the main funcion to show up with the stars of the previous funcion. if that makes any sense.
Change this

cout << "*";

to this:

cout <<num<< ":*";

Not really sure what you want? Its unclear.


I see that you are trying to do this recursively. Its not a bad choice, personally I would've done a for loop. Whatever floats your boat.
Last edited on
how would you have done the for loop
i tried that but all that i get is
it displays the number each time it outputs the star

this is the output
1:*
2: * 1: *
etc
what i need to have output is this

1: *
2: **
5: *****
3: ***
4: ****
7: *******
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void output_stars(int num)
{
if (num > 0)
{
cout << num;
for(int i = 0; i<num; i++)
cout << "*";
}
else
{
cout << endl;
}
cout<<endl;
}
Thank you so much for the help
OH yeah! I would generally stay away from recursively loops, just for this reason. Goodluck.
Topic archived. No new replies allowed.