Here's how I'd do it. Examine some sample diamonds (5, 7 and 9 lines):
*
* ***
* *** *****
*** ***** *******
***** ******* *********
*** ***** *******
* *** *****
* ***
*
The first line consists on some spaces and some stars. How many? Number of stars is always one, number of spaces depends on the number of lines desired:
Number of lines | Initial space count
-----------------+---------------------
1 | 0
2 | 0
3 | 1
4 | 1
5 | 2
6 | 2
7 | 3
8 | 3
So the initial number of spaces is (#lines - 1) / 2, rounded down
.
The other thing you'll notice is that on each subsequent line, the number of spaces reduces by one and the number of stars increases by two.
That's up until the point where the number of spaces is zero, then you start increasing spaces by one and decreasing stars by two until the number of stars is once again one.
The only other special case is an even number of lines where you should duplicate the middle line.
Looking at your code, you're very close, you just have to adjust where the loop statements are in relation to what's being printed. In other words, the outer loop is for lines, then an inner loop for spaces followed by another inner loop for stars.
I wrote a test program in Python (see below) but you really shouldn't have to understand Python syntax (that program's a lot bigger than I originally thought it would be) to get some use from this answer so here's some simplified pseudo-code. You should always sit down and think out the problem before you start writing code. This will help you develop the skills that will do you well in the future.
Main:
Get numlines from user, check that greater than 0.
Set numstars to 1.
Set numspaces to int((numlines-1)/2)
call Output (numspaces,numstars)
while numspaces > 0:
numspaces = numspaces - 1
numstars = numstars + 2
call Output (numspaces,numstars)
if numlines is even:
call Output (numspaces,numstars)
while numstars > 0:
numspaces = numspaces + 1
numstars = numstars - 2
call Output (numspaces,numstars)
end.
Output(spaces,stars):
for i = 1 to spaces:
print " "
for i = 1 to stars:
print "*"
print end-of-line
return
Finally, here's the Python code I used to test the pseudo-code (not really giving you the code since you need C++):
import sys
# Construct line based on number of spaces and stars.
def outLine (spc,str):
# Start with empty line.
line = ""
# Add spaces.
for i in range(0,spc):
line = "%s "%(line)
# Add stars.
for i in range(0,str):
line = "%s*"%(line)
#Output line.
print line
# Get number of lines from user and check.
numlines = input ("Enter number of lines: ")
if numlines < 1:
print "Must be greater than zero"
sys.exit(1);
# Calculate initial space and star count.
numspaces = int ((numlines-1)/2)
numstars = 1
# Output initial line.
outLine (numspaces,numstars)
# Output subsequent lines until middle reached.
while numspaces > 0:
numspaces = numspaces - 1
numstars = numstars + 2
outLine (numspaces,numstars)
# Repeat middle if even number of lines desired.
if numlines % 2 == 0:
outLine (numspaces,numstars)
# Output the bottom half of the diamond.
while numstars > 0:
numspaces = numspaces + 1
numstars = numstars - 2
outLine (numspaces,numstars)
and here's an example run:
Enter number of lines: 15
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
*