Drawing a Diamond

This code is designed to Draw a Diamond, I've worked out all the bugs in the main Draw function, which is to be transferred to another project and used to interact with a Class. However I'm currently trying to make sure it works, The project runs and compiles, but doesn't output anything from my Draw(); function.

Here is the code:(please note that you'll see "/* << SPACES */, this is intentional and will be replaced after I can figure out the whole output issue, so right now it won't quite look like a diamond when it's outputed)

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <string>
#include <iostream>
using namespace std;



void Draw(int, char, char);

int main()//Testing Draw
{
	cout << "Testing Draw:\n";

	Draw(6, '#', '$');

	return 0;

}



void Draw(int size, char border, char fill)
{
	bool stop = false;
	bool control = false;//This Controls which direction is drawing the Diamond
	int count = size;
	int iterationCount = 1;
	int fillCount = 1;

	if(size == 1)//Special Diamond of Size 1
	{
		cout << border << endl;
	}
	else if(size == 2)//Special Diamond of Size 2
	{
		cout << border << endl;
		cout << border << ' ' << border << endl;
		cout << border << endl;
	}
	else if(size > 2)//All other Diamonds of sizes from 3 to 39
	{
	do
	{//Start of Loop

		//Controls Counter Directions
		if(control = false)//Increments counter down to draw to center
		{	count--; iterationCount++; fillCount = fillCount + 2;}
		else if(control = true)//Increments the counter back up
		{	count++; iterationCount--; fillCount = fillCount - 2;}
		//End Counter Direction Control

		if(count > 1)//Counts down from size, to 2
		{
			if(iterationCount == 1)//Special Case, prints out 1st layer, top and bottom
			{
				cout /*<< SPACES*/ << border << endl;
			}
			else if(iterationCount == 2)//Special Case, prints out 2nd layer, top and bottom
			{
				cout /*<< SPACES*/<< border << endl;
				cout /*<< SPACES*/<< border << ' ' << border << endl;
				cout /*<< SPACES*/<< border << endl;
			}
			else if(iterationCount > 2)//All other cases, through 39
			{
				cout /*<< SPACES*/<< border;
				for(fillCount; fillCount <= fillCount + 2; fillCount++)//Draws The fill characters, Fill count is controlled by
					/*There should be one less fill character for each space*/
				{
					if(fillCount % 2 == 1)//If count is an odd, write a space
						cout << ' ';
					else
						cout << fill;//If count is even, write a fill
				}
				cout /*<< SPACES*/<< border << endl;
				
				if(count == 1)//Draws center and Initializes BOOL to true, reversing the drawing
					{
						control = true;
					}
			}					
		}
		else if(count > size)//If the count is greater than size, Stop Drawing
			cout << "\nDiamond is complete\n";
			stop = true;//
	}while(stop = false);
	}





}
I didn't look in detail, but right away I notice you are confusing assignment (=) with comparison (==).

Lines 45, 47, and 85. Possibly others... those are just the ones I noticed.

EDIT: also your indentation is incorrect, leading you to have incorrect assumptions about some statements. For example line 84, even though you [incorrectly] indented it... is not part of that else statement (because the else statement does not have braces), and therefore it will be executed every loop iteration... not just when count > size.


Fix up your indentation. Add braces where appropriate. Fix the ='s mistakes. Then see if it's still busted.
Last edited on
Thank you very much! I've been at it for a few hours and knew that there had to of been something simple like that (the ='s) that I kept missing. That aside, I can definitely see an output now, although it's only the first few lines until it get's stuck inside the loop at 66, any more advice would be greatly appreciated!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
				for(fillCount; fillCount <= fillCount + 2; fillCount++)//Draws The fill characters, Fill count is controlled by
					/*There should be one less fill character for each space*/
				{
					if(fillCount % 2 == 1)//If count is an odd, write a space
						cout << ' ';
					else
						cout << fill;//If count is even, write a fill
				}
				cout /*<< SPACES*/<< border << endl;
				
				if(count == 1)//Draws center and Initializes BOOL to true, reversing the drawing
					{
						control = true;
					}
Last edited on
fillCount <= fillCount + 2;

fillCount will always be less than itself+2

This will loop forever until fillCount overflows to a negative number..


EDIT:

Also, this might be a good opportunity to learn how to debug. I give a general step-by-step in another thread here:

http://www.cplusplus.com/forum/beginner/75304/#msg403990
Last edited on
Again, Thank you!!! A second set of eyes is always a great thing to have!
Topic archived. No new replies allowed.