I have to write a program for my C++ class which tells you what kind of triangle it is after you input three sides.
I have written the program in a rough shape, it can determine what kind of triangle it is, but if I input 0 or a negative number it doesn't output the error message for user.
Any suggestions? I tried rewriting it differently, using websites and my book, but I cannot figure it out. :( Any suggestions?
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
string threeInitials;
double a, b, c;
cout << "Insert your three initials. ";
cin >> threeInitials;
cout << "Insert length of three sides of a triangle.\n""Remember that you cannot have negative numbers.\n\n";
cout << "Insert A ";
cin >> a;
cout << "Insert B ";
cin >> b;
cout << "Insert C ";
cin >> c;
cout << "Your three initials are " << threeInitials << endl;
cout << "Your three sides of triangle are " << a << ", " << b << " and " << c << endl;
if (a>0 && b>0 && c>0)
if ((a==b) && (b==c))
{
cout << "Your triangle is Equilateral.\n""It is not a right triangle." << endl;
}
elseif (a!=b && b!=c && a!=c)
{
cout << "Your triangle is Scalene.\n""It is not a right triangle." << endl;
}
elseif (a==b || b==c || a==c)
{
cout << "Your triangle is Isosceles.\n""It is not a right triangle." << endl;
}
elseif ((a*a) + (b*b) == (c*c) &&
(c*c) + (b*b) == (a*a) &&
(a*a) + (c*c) == (b*b))
{
cout << "Your triangle is Right-Angled.\n""It is a right triangle." << endl;
}
else
{
cout << "Your entered numbers for one or more sides lengths cannot form a triangle.\n""Please review your numbers and try again." << endl;
}
system ("pause"); // may be necessary with Express version.
return 0;
}
Added Bold Braces.Check now....
if (a>0 && b>0 && c>0){
if ((a==b) && (b==c))
{
cout << "Your triangle is Equilateral.\n"
"It is not a right triangle." << endl;
}
else if (a!=b && b!=c && a!=c)
{
cout << "Your triangle is Scalene.\n"
"It is not a right triangle." << endl;
}
else if (a==b || b==c || a==c)
{
cout << "Your triangle is Isosceles.\n"
"It is not a right triangle." << endl;
}
else if ((a*a) + (b*b) == (c*c) &&
(c*c) + (b*b) == (a*a) &&
(a*a) + (c*c) == (b*b))
{
cout << "Your triangle is Right-Angled.\n"
"It is a right triangle." << endl;
}}