These are the instructions: Write an interactive program to have a user input th
ID: 3624520 • Letter: T
Question
These are the instructions:
Write an interactive program to have a user input their three initials and the length of the three sides of a triangle.
Determine if the entered sides form a valid triangle (no negative sides), and what type of triangle (isoceles, scalene and equilateral)
Check data for validity.
Check for valid initials and if any data is incorrect then do not continue to run the program.
I am having trouble with the initials part.
#include
using namespace std;
int main()
{
char initial1, initial2, initial3;
int side1, side2, side3;
cout<<"Enter three alphabetical initials:";
cin >>initial1>>initial2>>initial3;
if (('A' <= initial1 && initial1 <= 'Z') || ('a' <= initial1 && initial1 <= 'z'))
{
if (('A' <= initial2 && initial2 <= 'Z') || ('a' <= initial2 && initial2 <= 'z'))
{
if (('A' <= initial3 && initial3 <= 'Z') || ('a' <= initial3 && initial3 <= 'z'))
{
cout<<"Enter three sides of a triangle:";
cin >>side1>> side2>> side3;
}}
else
{
cout << "Initials are invalid.";
}
/*Only do work if all three sides are positive non-zero values*/
if((side1 >= 0) && (side2 >= 0) && (side3 >= 0))
{
/*Next we check if the triangle inequality property is respected*/
if( (side1+side2 > side3) && (side2+side3 > side1) && (side1+side3 > side2))
{
/*Next we check for special triangles - Isosceles or Equilaterals*/
if((side1 == side2) || (side2 == side3) || (side3 == side1))
{
/*Check for special case of Isosceles ALL lenths are equal*/
if((side1 == side2) && (side2 == side3) && (side3 == side1))
{
cout << " This is an Equilateral Triangle.";
}
/*Otherwise it's just a regular Isosceles*/
else
{
cout << " This is an Isosceles Triangle";
/*Next we check for right angles in Isoceles*/
/* applying pythagorean here as (a*a + b*b = c*c) */
if( (side1*side1 + side2*side2 == side3*side3) || (side1*side1 + side3*side3 == side2*side2) || (side3*side3 + side2*side2 == side1*side1))
{
cout << " This Isosceles Triangle has a right angle.";
}
}
}
/*If neither of the 3 sides are equal pairs then this is a regular triangle*/
else
{
cout << "This is a Scalene Triangle";
/*Next we check for right angles in Scalenes*/
/* applying pythagorean here as (a*a + b*b = c*c) */
if( (side1*side1 + side2*side2 == side3*side3) || (side1*side1 + side3*side3 == side2*side2) || (side3*side3 + side2*side2 == side1*side1))
{
cout << " This Scalenes Triangle has a right angle.";
}
}
}
/*Otherwise we print an inequality error message */
else
{
cout << " A triangle's three sides must respect the triangle inequality.";
}
}
/*Otherwise print a negatiev-sides error message*/
else
{
cout << " A triangle needs to have 3 sides of non-negative length.";
}
}
system("pause");
return 0;
}
Explanation / Answer
I'm not incredibly good at C++, but I can read it. I noticed a couple things, possibly neither will help, but another set of eyes helps. First of all, it looks like you didn't import any libraries, the #include is just blank. I'm not sure if that changes anything though. And a small issue, the comments claimed that you are waiting for 3 positive, non-zero numbers, but you have it accepting any number greater than OR EQUAL TO zero. That'll cost you if your professor is trying to break your program. If neither of these work, I'll happily take a look at it again.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.