Help printing a triangle

Hi there. Hoping someone can explain what I am doing wrong.
This is supposed to out a triangle shape, but I am just getting a straight line.

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
  #include <iostream>
#include <iomanip>

using namespace std;

int length;
int i,j,k;
char symbol=1;

int main ( ) 
{
    cout << "Enter a value to represent the base of a triangle shape (not to exceed 80): ";
    cin >> length;
    length = length + 1;
    cout << "\n";
    
    cout << "Enter the character to be used to generate the filled-in triangle shape (for eg., #, * $): ";
    cin >> symbol;
    cout << "\n";
    
    for(i = length; i <= length; i++) 
    {
        for (j = length; j > i; j--)
			cout<<' ';
			
	    for (k = length; k < 2*i; k++)
			cout<<'*';
		
		cout << endl;
    }
    
}


OUTPUT:
Enter a value to represent the base of a triangle shape (not to exceed 80): 11

Enter the character to be used to generate the filled-in triangle shape (for eg., #, * $): *

************
Hi,
1
2
3
4
5
6
7
8
9
10
for(i = length; i <= length; i++) 
    {
        for (j = length; j > i; j--)
			cout<<' ';
			
	    for (k = length; k < 2*i; k++)
			cout<<'*';
		
		cout << endl;
    }


==>
1
2
3
4
5
for (int i = length; i >= 1; i--)
{
    for (int j = i; j >= 1; j--) cout << symbol;
    cout << endl;
}
Last edited on
Also :
1
2
3
cin >> length;
length = length + 1;
cout << "\n";
> This is supposed to out a triangle shape, but I am just getting a straight line.
for(i = length; i <= length; i++) {}

Now (i = length).
When your program finishes the first loop, (i) is increased by 1 (now (i > length)). When your program enters the next loop, it will check the condition (i <= length) which is always false, hence breaking the loop.
Does that help you? :)
Hi there, thanks so much for your help. That makes sense, but now I am getting an infinite output of *'s, instead of just a line. How can I make this become a triangle instead?
> How can I make this become a triangle instead?
Try this again. It is still the same thing as before, but this one will do what you want :)

1
2
3
4
5
for (int i = length; i >= 1; i--)
{
    for (int j = i; j >= 1; j--) cout << symbol;
    cout << endl;
}
Last edited on
Does that? : )
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
  #include <iostream>
#include <iomanip>

using namespace std;

int length;
int i,j,k;
char symbol=1;

int main ( ) 
{
    cout << "Enter a value to represent the base of a triangle shape (not to exceed 80): ";
    cin >> length;
    length = length + 1;
    cout << "\n";
    
    cout << "Enter the character to be used to generate the filled-in triangle shape (for eg., #, * $): ";
    cin >> symbol;
    cout << "\n";
    
    for(i = 0; i <= length; i++) // <--
    {
        for (j = length; j > i; j--)
			cout<<' ';
			
	    for (k = length; k < 2*i; k++)
			cout<< symbol; // <--
		
		cout << endl;
    }
    
}


Enter a value to represent the base of a triangle shape (not to exceed 80): 10

Enter the character to be used to generate the filled-in triangle shape (for eg., #, * $): f

           
          
         
        
       
      
     *
    ***
   *****
  *******
 *********
***********
Last edited on
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
#include <iostream>

int main ( )
{
    int length = 0;
    char symbol = '*';
    bool keep_going = true;
    
    do{
        std::cout << "Enter base size of triangle (1 - 80 inclusive): ";
        std::cin >> length;
        
        if ( length < 1 || length > 80)
            keep_going = true;
        else
        {
            std::cout << "Enter symbol to display triangle (eg., #, * $): ";
            std::cin >> symbol;
            
            keep_going = false;
        }
    }while (keep_going == true);
    
    for( int i = 0; i <= length; i++)
    {
        for(int j = 0; j < length - i ; j++)
            std::cout << ' ';
        
        for(int k = 0; k < 2 * i - 1; k++)
            std::cout << symbol;
        std::cout << '\n';
    }
    return 0;
}
closed account (48bpfSEw)
With animation:

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 DRAW TRIANGLE
 2016 by Necip
**/

#include <windows.h>
#include <iostream>
#include <conio.h>
#include <time.h>

/*********************
 SYSTEM HELP FUNCTIONS
 *********************/  

/**
 Sleep process to slow down application
 **/
void sleepcp(int milliseconds) {
    clock_t time_end;
    time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000;
    while (clock() < time_end) { }
}
  
/**
 Get Key Stroke
 **/  
char getkey (void) {
  if (kbhit()) {
    char c = getch();
    if (c != '\0')
      return c;
    }  
  sleepcp(100);
  return 0;
  }
  
/**
 Goto position x,y on display
 **/  
void gotoPos (int x, int y) {
  COORD c = {x,y};
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}

/**
 Print string on x,y at display
 **/  

void printPos (int x, int y, char* pszBuffer) {
  gotoPos (x,y);
  std::cout << pszBuffer;
  }

/**
 Print char on x,y at display
 **/    
void printPos (int x, int y, char c) {
  gotoPos (x,y);
  std::cout << c;
  }
    
      
/*********************
 GAME DEFINTIONS
 *********************/  
      
/**
 Characters of the input file
 **/       
const char CH_WALL    = 'W'; 
const char CH_GROUND  = 'G'; 
const char CH_STONE   = 'O'; 
const char CH_SPACE   = ' '; 
const char CH_EXIT    = 'X'; 
const char CH_PLAYER  = 'P'; 
const char CH_DIAMOND = '*';  

/**
 Size of play area
 **/ 
const int  MAXX         = 70;
const int  MAXY         = 20; 

/**
 Input File Content (example) 
 Area must have the size of MAXX, MAXY!
 
 W : Wall     (the frame is obligatory!)
 G : Ground
 * : Diamonds
 O : Stone
 P : Player
 X : Exit
 
 **/ 
               //  1234567890123456789012345678901234567890123456789012345678901234567890
char* ptrBuffer = "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW" // 1
                  "W                                 P                                  W" // 2
                  "W                                                                    W" // 3
                  "W                                                                    W" // 4
                  "W                                                                    W" // 5
                  "W                                                                    W" // 6
                  "W                                                                    W" // 7
                  "W                                                                    W" // 8
                  "W                                                                    W" // 9
                  "W                                                                    W" // 0
                  "W                                                                    W" // 1
                  "W                                                                    W" // 2
                  "W                                                                    W" // 3
                  "W                                                                    W" // 4
                  "W                                                                    W" // 5
                  "W                                                                    W" // 6
                  "W                                                                    W" // 7
                  "W                                                                    W" // 8
                  "W                                                                    W" // 9
                  "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW";// 0                                                         

/**
 Characters converted for display
 **/ 
const char CHAR_WALL    = 219; // 'W';    // ascii 219 █     
const char CHAR_SPACE   = ' ';    
const char CHAR_PLAYER  = 244; // '@';    // ascii 244 @
const char CHAR_DIAMOND = '*'; // '*';    

/**
 User Input Key Strokes
 **/ 
const char CHAR_QUIT    = 'q';      

/*********************
 GAME.H
 *********************/ 
class Game {
public:
  int  px,py;                 // x,y of player
  char cell[MAXX][MAXY];      // Game field

public:  
  Game();
 
  void load (char* pszBuffer);  // loads next level
  void show (void);             // shows game field (cells)
  void exec (int iSize);        // executes game
  
private:
  void anim (void);                               // animates next frame
  void swap (int x1, int y1, int x2, int y2);     // swaps cell contents
  };


/*********************
 GAME.CPP
 *********************/ 

/**
 **/ 
Game::Game() {
  px = -1;
  py = -1;
}
 
/**
 **/
void Game::show (void) {
  for (int y = MAXY-1; y >= 0; y--) {
    for (int x = MAXX-1; x >= 0; x--) {
      printPos (x,y, cell[x][y]); 
      }
    }
  }
    
/**
 **/
void Game::load (char* pszBuffer) {

  int x=0;
  int y=0;
  for (char* ptr = pszBuffer; *ptr; ++ptr, ++x) {
    if (x >= MAXX) {
      x = 0;
      y ++;
      }    

    switch (*ptr) {
      case CH_WALL:
        cell[x][y] = CHAR_WALL;   
        break;
      case CH_SPACE:
        cell[x][y] = CHAR_SPACE;   
        break;
      case CH_PLAYER:
        cell[x][y] = CHAR_PLAYER;   
        break;  
      }
        
    if (cell[x][y] == CHAR_PLAYER) {
      px = x;
      py = y;
      }
    }
  }
 
/**
 **/
void Game::swap(int x1, int y1, int x2, int y2) {

  char cTmp     = cell[x1][y1];
  cell[x1][y1]  = cell[x2][y2];
  cell[x2][y2]  = cTmp;

  printPos (x1,y1, cell[x1][y1]); 
  printPos (x2,y2, cell[x2][y2]);  
} 
 
/**
 **/
void Game::anim (void) {

  // User Input
  char ck = getkey ();    
  switch (ck) {
    case CHAR_QUIT:
      exit (0);
      break;
    }
  
  // Animation  
  for (int y = MAXY-2; y > 0; y--) {
    for (int x = MAXX-2; x > 0; x--) {
      char c          = cell[x  ][y  ];      
      char cDownLeft  = cell[x-1][y+1];
      char cDownRight = cell[x+1][y+1];
      char cDown      = cell[x  ][y+1];
      
      switch (c) {
        case CHAR_DIAMOND :
          if (cDown == CHAR_SPACE)
            swap (x, y, x, y+1);
          else if (cDownLeft == CHAR_SPACE)
            swap (x, y, x-1, y+1);
          else if (cDownRight == CHAR_SPACE)
            swap (x, y, x+1, y+1);
          break;
        }
      }
    }
    
    printPos (0, MAXY, "q: quit");
  }

/**
 **/
void Game::exec (int iSize) {
  for (int i=0; i < iSize; i++) {      
    cell[px][py] = CHAR_DIAMOND;
    anim ();
    }
    
  while (true) {
    anim ();
    }
  }
  
  
/*********************
 MAIN.CPP
 *********************/ 
int main (void) {

  system ("cls");
  
  printPos (0, MAXY+1, "Input size of triangle (1-10): ");
  int iSize=0;
  std::cin >> iSize;
  
  if (iSize <1 || iSize >10) {
    printPos (0, MAXY+2, "Input out of range!");
    return -1;
    }
  
  
  int iCountDiamonds = 0;
  int iDelta         = 1;
  for (int i=0;i<iSize;i++) {
    iCountDiamonds += iDelta;
    iDelta += 2;
    }
  
  Game game;
  game.load (ptrBuffer);
  game.show ();
  game.exec (iCountDiamonds);

  
  printPos (0, MAXY+1, "Finished");
   
  return 0;
}
@Necip
+1
Topic archived. No new replies allowed.