My asterisk diamond is printing out wrong.

I feel as if I'm so close and I'm just messing up a value somewhere, but I am at a loss as to what I should change.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "stdafx.h"
#include <iomanip>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	int spaces = 15;
	int divider;

	cout << "Please enter an odd integer" << endl;
	cin >> n;

	divider = n / 2;

	//Top half of diamond
	for (int i = 0; i < n; i++)
	{
		if (i <= divider + 1)
		{

			cout << setw(spaces);
			spaces--;

			for (int j = 0; j < 2 * i - 1; j++)
			{
				cout << "*";
			}
			cout << endl;
		}
	}

	for (int i = n;  i > divider; i--)
	{
		if (i > divider)
		{
			cout << setw(spaces);
			spaces++;
			for (int j = 0; j < 2 * i + -1; j++)
			{
				cout << "*";
			}
			
		}
		cout << endl;
	}


	return 0;
}
Can you please be more specific, print a screenshot or paste output if it visibly displays the issue.

Your code doesn't compile for me in GCC, I bet it's borland/Codegear..
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//#include "stdafx.h" // CHANGE ***************************************
#include <iomanip>
#include <iostream>

using namespace std;

int main() // CHANGE ************************************************
{
	int n;
	int spaces = 15;
	int divider;

	cout << "Please enter an odd integer" << endl;
	cin >> n;

	divider = n / 2;

	//Top half of diamond
	for (int i = 0; i < n; i++)
	{
		if (i <= divider + 1)
		{

			cout << setw(spaces);
			spaces--;

			for (int j = 0; j < 2 * i - 1; j++)
			{
				cout << "*";
			}
			cout << endl;
		}
	}

	for (int i = n;  i > divider; i--)
	{
		if (i > divider)
		{
			cout << setw(spaces);
			spaces++;
			for (int j = 0; j < 2 * i + -1; j++)
			{
				cout << "*";
			}
			
		}
		cout << endl;
	}


	return 0;
}


This works if you run it here with a few small changes. It looks as though you are using Visual Express :)
Last edited on
Topic archived. No new replies allowed.