Triangle, logical and relational operators

closed account (42hU7k9E)
Basicly, i have to write a program which reads the lenght of all 3 sides of an triangle, and the program finds out if the triangle can be drawn. The basic rule, to find out if the triangle is logical, is a+b>c. I know that. But im having a program with coding the program.

#include <iostream.h>
int main()
{
int a, b, c;
cout << "Put in a, b, and c: ";
cin >> a >> b >> c;

if((a+b)>c && (c+b)>a && (a+c)>b)
cout << "Triangle can be drawn";
else
cout << "The triangle can't be drawn" << endl;
return 0;
}
Wich compiler do you use? My compiler (dev-c++) dont take iostream.h, and you need to either use using namespace std; at the beginning of the code or
1
2
std::cout<<
std::cin>>
if you use cout/cin.

You must use { and } around the if and else statement.

This is the new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
int a, b, c;
cout << "Put in a, b, and c: "<<"\n";
cin >> a >> b >> c;

if((a+b)>c && (c+b)>a && (a+c)>b)
{
cout << "Triangle can be drawn";
}
else
{ 
cout << "The triangle can't be drawn" << endl;
}
return 0;
}


It works just fine with my compiler, you may have to change some stuff.

Hope this helps
Btw,

My program above works fine for whole numbers, but i think that in this case you most of the time wont get that, but someting like 1.23. Maybe you should change the type from a-b-c into double?
Notice that the if statement could work without curly braces if it has just a line of code in its block
Yes. I always use braces, to avoid mistakes (eg if i add some lines later), but you're right
Topic archived. No new replies allowed.