Triangle problem

I'm new and I'm trying to figure out the problems with this triangle but can't after a week of trying. If I could just see the right code I think I would understand this really good.

#include <iostream>

int drawBar(int);

int main()
{
std::cout << std::endl << "Let's Draw a triangle!\n";

//determine how many lines will be drawn
int triangleBase = 0;

//draw the triangle
for (int i = 0 ; i >= triangleBase ; i--) {
drawBar(i);
}

return 0;
} //end main

int drawBar(int barSize) {
//draws a line of asterisks
//the number of asterisk drawn equals barSize

int theCounter = 0;
while (theCounter >= barSize) {
theCounter--;
std::cout << '*';
}
std::cout << '\n';
return 0;
} //end drawBar
why is the function an int if its return value has no impact? (Make it a void).

Why initiate i at 0 and then check if it's larger than triangleBase (which is also being initiated at 0, try swapping some numbers around, such as do this whilst i is smaller than triangleBase)

since I had nothing better to do i fixed the code for you aswell
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>

void drawBar(int);

using namespace std;

int main()
{
cout << endl << "Let's Draw a triangle!\n";

//determine how many lines will be drawn
int triangleBase = 5; // Initiate at random value

//draw the triangle
for (int i = 1 ; i <= triangleBase ; i++) // While i is smaller than or equal to triangleBase - Edit: realised there's no point starting at 0
{
  drawBar(i);
}
system("PAUSE"); // Crude but works for personal projects
return 0;
} //end main

void drawBar(int barSize) 
{

    while (barSize != 0) 
    {
      barSize--;
      cout << '*'; 
    }
    cout << endl;
}
Last edited on
Topic archived. No new replies allowed.