Triangle program

Hi guys, I'm having trouble with this program.
Question is write a c++ program to draw and fill a triangle given three sides.
For some values, it works fine but for some, it doesn't. For example, if I enter 5, 6 & 9 it works fine but when I enter 6, 8 & 9 it doesn't work.

#include<iostream>
using namespace std;
int main()
{
int rows, columns;
double l,m,n, largest_side;

cout<<"Enter three sides: ";
cin>>l>>m>>n;
largest_side=l;
if(m>=largest_side)
{
largest_side=m;
rows=largest_side;
if(l>=n)
columns=n;
else
columns=l;
}
if(n>=largest_side)
{
largest_side=n;
rows=largest_side;
if(l>=m)
columns=m;
else
columns=l;
}
else
{
rows=largest_side;
if(n>=m)
columns=m;
else
columns=n;
}

char c=215;
cout<<endl;
for (int i=0;i<=rows;i++)
{
char c=215;
for(int space=1;space<=rows-i;space++)
{
cout<<" ";
}

for(int j=0;j<2*columns-i;j++)
{
if(i==rows)
{
h++;
}
}
for(int j=0;j<2*columns-i-h;j++)
{

cout<<"*";
}
cout<<endl;
}
return 0;
}

Last edited on
please use code tags
SamuelAdams What?
@Muhammad Kashif
he's referring to those formats on the right (when you post something here)
and that you should use [ code ] (without spaces between the word and brackets), and then end the code with [ /code ] (also without the spaces), so that it can be more readable.
something like this:
[ code ]
code
more code
[ /code ]

you could press one of the buttons, and see what option it is (<> is the one for source code)
and so the result should look like this: (also indent the text if needed)
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
#include<iostream>
using namespace std;
int main()
{
    int rows, columns;
    double l,m,n, largest_side;
    cout<<"Enter three sides: ";
    cin>>l>>m>>n;
    largest_side=l;
    if(m>=largest_side)
    {
        largest_side=m;
        rows=largest_side;
        if(l>=n)
            columns=n;
        else
            columns=l;
    }
    if(n>=largest_side)
    {
        largest_side=n;
        rows=largest_side;
        if(l>=m)
            columns=m;
        else
            columns=l;
    }
    else 
    {
        rows=largest_side;
        if(n>=m)
            columns=m;
        else
            columns=n;
    }
    char c=215;
    cout<<endl;
    for (int i=0;i<=rows;i++)
    {
        char c=215;
        for(int space=1;space<=rows-i;space++)
        {
            cout<<" ";
        }
        for(int j=0;j<2*columns-i;j++)
        {	
            if(i==rows)	
            {	
                h++;
            }
        }
        for(int j=0;j<2*columns-i-h;j++)
        {
            cout<<"*"; 
        } 
        cout<<endl;
    }
    return 0;
}

Edit: also there's a "h++" around line 50, and it hasn't previously been declared. If you meant to type another letter there, it's for the best if you change it in the post then
Last edited on
h is used in the for loop too. But what is it?

Registered users can post here. Sign in or register to post.