creating a diamond shape

I am trying to create a diamond shape, I wrote this function to created it but I am only getting the bottom half of the diamond, the top part is giving me numbers, below is my code.please help

void Diamond::printDiamond()
{
const int N=21;
int length;
char shape;

cout << "enter diamond shape and size: ";
cin >> shape >> length;

if (length > N)
{
cout << " You have entered an illegal value for the length. It must be between 1 and 21 " << endl;
}
else

for (int i = 1; i < length; i+=2)
{
for (int j = 1; j <= length - i; j+=2)
cout << ' ';

for (int k=0; k <= i; k++)
cout << shape;
cout << endl;
}

for ( int i=0; i < length; i+=2)

{
for (int j = 1; j<=i+2; j+=2)
cout << ' ';
for (int k=1; k<=length - i; k++)
cout << shape;
cout << endl;
}


}
Please use code tags.

You are missing curly braces in the else statement.

Also for (int i = 1; i < length; i+=2) always creates an up half with even symbols per row
You could use for (int i=!(length%2); i < length; i+=2)


It seems that you are using a class diamond. Why don't just put length and shape as attributes instead of getting them in the method.
This way the range test of length will be outside of print and get better error handling. (think in responsibilities)
Topic archived. No new replies allowed.