Help? Drawing Shapes in C++ (with characters)

I'm trying to make a code that can draw a square or triangle using characters. The user would specify the shape wanted, the character used, and the size.
ex: Square, #, and 8
########
########
########
########
########
########
########
########
########
or Triangle, @ and 6.
@
@@
@@@
@@@@
@@@@@
@@@@@@
@@@@@
@@@@
@@@
@@
@

Here's my code so far. I got the menu and inputs. Just gotta do the technical end of things...like drawing the shapes. lol
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
#include <iostream>
using namespace std;

int main()
{

int displayChar;
int lineLength;
char draw;


cout << "******************" << endl;
cout << "*  1 - Triangle  *" << endl;
cout << "*  2 - Square    *" << endl;
cout << "*                *" << endl;
cout << "*  0 - EXIT      *" << endl;
cout << "******************" << endl;

cout << "\n Enter Option Number: ";
    cin  >> draw;
	 
	 cout << "\n Enter character to draw shape with: ";
    cin  >> displayChar;

cout << "\n Enter the line length: ";
cin >> lineLength;


switch (draw)
{
// Draw triangle
case '1':
{
	for (int i=0; i<lineLength ; i++) 
	{
		cout << endl; 
                              
		for (int j=1, k=lineLength; j <= lineLength, k >= 1; j++, k--)
		{
			cout << displayChar;
		}
	}
}


// Draw square
case '2':
{
	for (int i=0; i<lineLength ; i++)
	{
		cout << endl; 
                              
		for (int j=0; j < lineLength ; j++)
		{
			cout << displayChar;
		}
	}
}

}
}

Last edited on
Your square is more of a rectangle, just sayin'.

This is something that should easily be accomplished using a for loop:

http://cplusplus.com/doc/tutorial/control/

For this particular program, you may want to invest some time in nested loops as well.
Here is how to add a control for the square. Use the same methodology for the triangle, but change the for(j) loop to limit the number of characters printed on each line. That's the part that gets interesting.

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
#include <iostream>
using namespace std;

int main()
{

int displayChar;
int lineLength;
char draw;


cout << "******************" << endl;
cout << "*  1 - Triangle  *" << endl;
cout << "*  2 - Square    *" << endl;
cout << "*                *" << endl;
cout << "*  0 - EXIT      *" << endl;
cout << "******************" << endl;

cout << "\n Enter Option Number: ";
cin  >> draw;
	 
cout << "\n Enter character to draw shape with: ";
cin  >> displayChar;

cout << "\n Enter the line length: ";
cin >> lineLength;

// Draw square
if (draw == 2) // Use an if or a switch to determine which shape we are drawing
{
	for (int i=0; i<lineLength ; i++) // each i represents a row (we will have lineLength rows)
	{
		cout << endl; //This adds the space at the end of the row 
                              //you could do this at the end but you seem to do \n at the start of each statement
		for (int j=0; j < lineLength ; j++) // This writes a character lineLength times.
		{
			cout << displayChar;
		}
	}
}

}
Last edited on
Updated the OP. Now when I try doing the triangle it comes out with a big number repeating.
???
Topic archived. No new replies allowed.