make a pyramid using loops

I am trying to create the pyramid (with 4 rows) using while loops. I think I have the idea correct but it's not outputting the pyramid right. I think it has mostly to do with the bracket placement and wrong source coding for the left and right side spacing. The pyramid should look like below (THE SLASHES ARE SPACES):

///*
//***
/*****
*******

Psuedocode:

Prompt user to enter a number of rows

Input 4 rows

Declare character asterisk

Prompt user to input a character for the pyramid

Input asterisk character

First while loop: While the quantity of rows is less than or equal to 4, create a new row. For each iteration of the first loop, increment by 1

Second while loop: While initial quantity of rows plus 1 is less than or equal to 4, increase the space by 1 for each iteration of the second loop

Third while loop: While 2 times the initial quantity of rows minus one is less than or equal to 4, print an asterisk. For each iteration of the third loop, increment by 1


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

#include <iostream>

using namespace std;

int main()

 

{

       cout << "Enter a number of rows:" << endl;

       int rows = 4;

       cin >> rows;

 

       cout << "Enter a character to draw the pyramid with:" << endl;

       char VIEW = '*';

       cin >> VIEW;

 

       //this section is for setting the rows

       int i = 1;//the i(initial) value (and value to increment by) is 1

 

       while (i <= rows)//while the most recent value of i is less than or equal to the amount of outputted rows

       {

              i++;//increment the most recent value of i by 1

 

 

              //this section is for the spacing

              int j = 1;//the j(initial) value (and value to increment by) is 1

 

              while (j <= rows - i)//while the most recent value of j is less than or equal to the amount of outputted rows minus the most recent value of i

 

              {

                     j++;//increment the most recent value of j by 1}

                     cout << " ";//output an empty space

 

 

                     //this section is for printing the characters

                     int k = 1;//the k(initial) value (and value to increment by) is 1

 

                     while (k <= (2 * i) - 1)//while k is less than or equal to 1 times the most recent value of i minus 1

 

                     {

                           k++;//increment the most recent value of k by 1}

                           cout << VIEW;// print character...

 

                           // go to new line...

                           cout << endl;

                     }

              }

       }

 
}
Your second two loops should be for loops and they should be on the same level.

ps: It's great that you're using so much whitespace, most people don't, but you could use a little less.
Last edited on
Works now! Thanks!
Topic archived. No new replies allowed.