Drawing shapes using for loops

Hi guys. I am working on a program that asks the user for an input on how large the shape should be, validates the input, and then displays the shape using asterisks. I figured out how to do the filled triangle but I don't know how to make inverted filled triangle, unfilled triangle, inverted unfilled triangle, filled rectangle, and unfilled rectangle. Could you help me out? 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// INCLUDE FILES
#include <iostream>
using namespace std;
int main()
{
    int no_lines,   // height of triangle or rectangle.
        width;      // width of rectangle.
    short choice;
    // ADDITIONAL VARIABLES
    
    // ADD A do-while LOOP TO THE PROGRAM SO THAT THE USER CAN REPEATEDLY
    // DISPLAY THE MENU, MAKE A CHOICE, AND HAVE THE APPROPRIATE STEPS FOR
    // THAT CHOICE CARRIED OUT.
    // THE LOOP SHOULD CONTINUE ITERATING UNTIL THE USER ENTERS 7 FOR THE MENU CHOICE.
    // HAVE THE PROGRAM PRINT SEVERAL BLANK LINES AFTER EACH SHAPE IS DRAWN.
        do
        {
        // Display menu.    
        cout << "THIS PROGRAM DRAWS VARIOUS SHAPES.\n" << endl;

        cout << "1. Filled Triangle" << endl;
        cout << "2. Filled Inverted Trianlge" << endl;
        cout << "3. Unfilled Triangle" << endl;
        cout << "4. Unfilled Inverted Trianlge" << endl;
        cout << "5. Filled Rectangle" << endl;
        cout << "6. Unfilled Rectangle" << endl;
        cout << "7. Exit\n" << endl;
        cin >> choice; 
        // ASK, READ AND VALIDATE CHOICE WITH while LOOP.
        
        // Process choice.
        switch (choice)
        {
               case 1: // Filled Triangle
                    // Ask, read and validate number of lines.
                    cout << "Enter the height for the filled triangle (2 - 75): ";
                    cin >> no_lines;
                    while (no_lines < 2 || no_lines > 75)
                    {
                          cout << "Number of lines has to be between 2 and 75.\n";
                          cout << "Enter the height for the filled triangle (2 - 75): ";
                          cin >> no_lines;
                    }
                    cout << endl;
                    // Draw shape.
                    // An example of a filled triangle with height 5
//                        *
//                        **
//                        ***
//                        ****
//                        *****
                    int i, j; 
                    for (i = 1; i <= no_lines; i++)
                    {
                        for (j = 1; j <= i; j++)
                            cout << "*";
                        cout << endl;
                    }
                    break;
               case 2: // Inverted filled triangle
                    // ASK, READ AND VALIDATE HEIGHT.
                    // DRAW INVERTED TRIANGLE
                    // AN EXAMPLE OF AN INVERTED TRIANGLE OF HEIGHT 10
//                        **********
//                        *********
//                        ********
//                        *******
//                        ******
//                        *****
//                        ****
//                        ***
//                        **
//                        *
                    break;
               case 3: // Unfilled triangle
                    // ASK, READ AND VALIDATE HEIGHT.
                    // DRAW UNFILLED TRIANGLE
                    // AN EXAMPLE OF A TRIANGLE OF HEIGHT 8
//                        *
//                        **
//                        * *
//                        *  *
//                        *   *
//                        *    *
//                        *     *
//                        ********
                    break;
               case 4: // Unfilled inverted triangle
                    // ASK, READ AND VALIDATE HEIGHT.
                    // DRAW UNFILLED INVERTED TRIANGLE
                    // AN EXAMPLE OF AN UNFILLED INVERTED TRIANGLE OF HEIGHT 7
//                        *******
//                        *    *
//                        *   *
//                        *  *
//                        * *
//                        **
//                        *

                    break;
               case 5:
                    // ASK, READ AND VALIDATE HEIGHT.
                    // ASKK, READ AND VALIDATE WIDTH.
                    // DRAW FILLED RECTANGLE
                    // AN EXAMPLE OF A 5 (HEIGHT) X 10 (WIDTH) FILLED RECTANGLE.
//                        **********
//                        **********
//                        **********
//                        **********
//                        **********
                    break;
               case 6:
                    // ASK, READ AND VALIDATE HEIGHT.
                    // ASKK, READ AND VALIDATE WIDTH.
                    // DRAW UNFILLED RECTANGLE
                    // AN EXAMPLE OF A 4 (HEIGHT) X 8 (WIDTH) UNFILLED RECTANGLE
//                        ********
//                        *      *
//                        *      *
//                        ********
                    break;
               case 7:
                    cout << "Exiting ...\n";
                    return 0; 
                    break;
        }
        } while (choice >= 1 && choice <= 7); 
    system("pause"); 
    return 0;
}
    

    
Last edited on
Bump.
Any ideas guys? I tried but it seems harder than it looks.
It is? It looks like loops, tracking variables and other things.

you have a variable named width, which I don't see you get from the user. For a simple triangle for case 1 would take two loops. To me the width variable is the only one I would need, since it would dictate how many lines I would create too.

1
2
3
4
5
6
7
8
for(int nWcount = 0; nWcount < width; nWcount++)
{
      for(int nIndex = 0; nIndex < nWcount; nIndex++)
      {
              cout << "*";
      }
      cout << endl;
}


all of the other cases would follow similar code and use a few if statements.
Last edited on
Hint for the upside down triangle: Initialize your outer for loop at the number of lines and count down.

Here's case 4:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "give the height: ";
cin >> no_lines;

// first line:
for (int i = 0; i < no_lines; i++)
	cout << '*';                       // A whole bunch of stars for the first line
cout << endl;

// next lines:
for (int j = no_lines-1; j > 1; j--)       // counts from lines-1 to 1, note I'm counting down.
{
	cout << '*';                       // First star in a line
	for (int i = 0; i < j-2; i++)      // Add j-2 spaces,
	cout << ' ';                       // Writing the spaces
	cout << '*' << endl;               // Last star in a line
}

// last line
cout << '*' << endl;


You should be able to infer the others based on this type of logic.
Last edited on
Topic archived. No new replies allowed.