Nov 5, 2013 at 2:11am UTC
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.
Nov 5, 2013 at 2:30am UTC
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 Nov 5, 2013 at 2:32am UTC
Nov 5, 2013 at 2:33am UTC
how would you have done the for loop
Nov 5, 2013 at 2:39am UTC
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: *******
Nov 5, 2013 at 2:47am UTC
Thank you so much for the help
Nov 5, 2013 at 2:58am UTC
OH yeah! I would generally stay away from recursively loops, just for this reason. Goodluck.