I would suggest you to do a dry run on your code if you possibly can..
Remember, in these shape structures, the outer loop is mostly always for the total number of rows.. Whereas the inner loops are for columns or to print asterisks.
From your code, notice the first outer loop,
1 2 3 4 5
|
for (a=0; a < 15; a++)
{
......
cout << endl // this cout << endl gives you the answer to your question that how does it know when to do row
}
|
You can see that at the end of the loop, there is an endl statement which tells the program to go the next line... As the loop will run for a total of 15 times, the endl statement will also execute 15 times so basically it will give you 15 rows...
It's similar for the columns.. Notice the inner loop,
1 2 3 4 5
|
for (b=0; b < 30; b++)
{
cout << "*"; // Print * (asterisk)
}
|
As you can see, that this loop will run for a total of 30 times every time... So, it will print 30 asterisks or in other words, will print a total of 30 columns.
EDIT: To your last question that why does it do 15 rows when the value is less than 15... That's because that the loop controlling variable (i.e. a in your program) is starting from 0... So loop runs from 0-14 which is equal to 15 times in total... If you simply change a value to a = 1 and make it a <= 15 then the program will perform the same function as it is performing right now