/*
4-70 Triangles
Write a program that will read three numbers (representing the lengths of the sides of a triangle)
and print out one of the following four words:
NONE if the three lengths do not represent the sides of a triangle
(i.e., if the sum of the lengths of any two sides is not greater than the third side)
GENERAL if the lengths specify a general triangle
(i.e., one whose sides are all of different lengths)
ISOSCELES if any two sides are of equal length
EQUILATERAL if all three sides of the triangle are of equal length
Write a program so that it will print out the lengths of the three sides
along with the type of triangle they represent. The program should process an arbitrary
number of sets of three numbers and will terminate upon reading a set of three numbers all of which are zero.
*/
#include <iostream>
usingnamespace std;
int main ()
{
bool yes = false;
do {
double a, b, c;
cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
cout << "What is side a: \n";
cin >> a;
cout << "What is side b: \n";
cin >> b;
cout << "What is side c: \n";
cin >> c;
if ( a=b=c=0)
{cout << "All three cannot be zero!\n";}
elseif ( a+b<c || b+c<a || a+c<b)
{cout << "Not a triangle! Try again!\n";
cout << "Side A is: " << "B is: " << "C is: " << endl;}
elseif (a != b != c)
{cout << "This is a general triangle!\n";
cout << "Side A is: " << "B is: " << "C is: " << endl;}
elseif (a=b || b=c || a=c)
{cout << "This is an isosceles triangle!\n";
cout << "Side A is: " << "B is: " << "C is: " << endl;}
else (a=b=c)
{cout << "This is an equilteral triangle!\n";
cout << "Side A is: " << "B is: " << "C is: " << endl;}
cout << "Do you want to continue (yes/no): \n";
} while (!yes);
cout << "Goodbye!\n";
system ("pause");
}
Heey guys! this is my code. i was wondering why i get this error 1>triangle.cpp(44): error C2106: '=' : left operand must be l-value at line 44
and 1>triangle.cpp(44): warning C4800: 'double' : forcing value to bool'true' or 'false' (performance warning)
and 1>triangle.cpp(41): warning C4805: '!=' : unsafe mix of type 'bool' and type 'double' in operation those errors! any help would be greatly appreciated! D:
#include <iostream>
usingnamespace std;
int main ()
{
bool no = false;
do {
double a=0, b=0, c=0;
cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
cout << "What is side a: \n";
cin >> a;
cout << "What is side b: \n";
cin >> b;
cout << "What is side c: \n";
cin >> c;
if ( a==b&&b==c&&c==0)
{cout << "All three cannot be zero!\n";}
elseif ( a+b<c || b+c<a || a+c<b)
{cout << "Not a triangle! Try again!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a != b != c)
{cout << "This is a general triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a==b || b==c || a==c)
{cout << "This is an isosceles triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
else (a==b&&b==c&&c==a);
{cout << "This is an equilteral triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
cout << "Do you want to try more? (yes/no): \n";
} while (!no);
cout << "Goodbye!\n";
system ("pause");
}
i think something is wrong with my do while loop :\ because i cant type yes or no. it just repeats! gaah!
and also, i tried plugging in a as 10, b as 10, and c as 15, but it tells me that its both equilateral and isosceles, when its supposed to be an isosceles. does anyone see my error?
Just like you did for a,b and c, you need to declare a variable such as char again and ask the user input "y" for yes and "n" for no. Once you've done that you need to make an if statement and check if(again == 'y') etc... just like you did with a,b and c.
#include <iostream>
#include <limits>
usingnamespace std;
int main ()
{
bool no = false;
char again;
do {
double a=0, b=0, c=0;
cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
cout << "What is side a: \n";
cin >> a;
cout << "What is side b: \n";
cin >> b;
cout << "What is side c: \n";
cin >> c;
if ( a==b&&b==c&&c==0)
{
cout << "All three cannot be zero!\n";
}
elseif ( (a+b)<c || (b+c)<a || (a+c)<b)
{
cout << "Not a triangle! Try again!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
}
elseif (a != b && b != c)
{
cout << "This is a general triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
}
elseif (a==b || b==c || a==c)
{
cout << "This is an isosceles triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
}
elseif (a==b&&b==c&&c==a)
{
cout << "This is an equilteral triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
}
cout << "Do you want to try more? ('y' for yes/'n' for no): \n";
cin>> again;
if (again=='y')
no = false;
else
no=true;
} while (!no);
cout << "Goodbye!\n";
cin.sync();
cout<<"Press Enter Key To Exit"<<endl;
cin.ignore( numeric_limits<streamsize>::max(),'\n' );
}
Krahl (42) Mar 7, 2012 at 7:53pm
Just like you did for a,b and c, you need to declare a variable such as char again and ask the user input "y" for yes and "n" for no. Once you've done that you need to make an if statement and check if(again == 'y') etc... just like you did with a,b and c.
Post back if you need more help.
okaay! i'm going blindly now~! i'm not sure what i did LOOL..
#include <iostream>
usingnamespace std;
int main ()
{
char again;
bool y = true;
do {
double a=0, b=0, c=0;
if (again == y)
{cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
cout << "What is side a: \n";
cin >> a;
cout << "What is side b: \n";
cin >> b;
cout << "What is side c: \n";
cin >> c;
if ( a==b&&b==c&&c==0)
{cout << "All three cannot be zero!\n";}
elseif ( a+b<c || b+c<a || a+c<b)
{cout << "Not a triangle! Try again!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a != b != c)
{cout << "This is a general triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a==b || b==c || a==c)
{cout << "This is an isosceles triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
else (a==b&&b==c&&c==a);
{cout << "This is an equilteral triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
cout << "Do you want to try more? (yes/no): \n";
cin >> again;
} while (!y);}
else (again == n)
{cout << "Goodbye!\n";
system ("pause");}
}
#include <iostream>
usingnamespace std;
int main ()
{
char again;
bool no = true;
do {
double a=0, b=0, c=0;
cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
cout << "What is side a: \n";
cin >> a;
cout << "What is side b: \n";
cin >> b;
cout << "What is side c: \n";
cin >> c;
if ( a==b&&b==c&&c==0)
{cout << "All three cannot be zero!\n";}
elseif ( a+b<c || b+c<a || a+c<b)
{cout << "Not a triangle! Try again!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a != b && a != c && b != c)
{cout << "This is a general triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a==b || b==c || a==c)
{cout << "This is an isosceles triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
else (a==b&&b==c&&c==a);
{cout << "This is an equilteral triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
cout << "Do you want to try more? (y/n): \n";
cin >> again;
if(again == 'n')
no = true;
} while (!no);
cout << "Goodbye!\n";
system ("pause");
return 0;
}
The reason why you weren't getting isosceles was because of the following line elseif (a != b != c)
#include <iostream>
usingnamespace std;
int main ()
{
char again, n;
do {
double a=0, b=0, c=0;
cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
cout << "What is side a: \n";
cin >> a;
cout << "What is side b: \n";
cin >> b;
cout << "What is side c: \n";
cin >> c;
if ( a==b&&b==c&&c==0)
{cout << "All three cannot be zero!\n";}
elseif ( a+b<c || b+c<a || a+c<b)
{cout << "Not a triangle! Try again!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a != b != c)
{cout << "This is a general triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a==b || b==c || a==c)
{cout << "This is an isosceles triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
else (a==b&&b==c&&c==a);
{cout << "This is an equilteral triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
cout << "Do you want to try more? (yes/no): \n";
cin >> again;
if (again == 'n')
n = true;
}while(!n);
cout << "Goodbye!\n";
system ("pause");
}
WOOOOW! THANK YOU KRAHL! IT WORKS NOW :D YOU ROCK!!!
#include <iostream>
usingnamespace std;
int main ()
{
char again, n;
do {
double a=0, b=0, c=0;
cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
cout << "What is side a: \n";
cin >> a;
cout << "What is side b: \n";
cin >> b;
cout << "What is side c: \n";
cin >> c;
if ( a==b&&b==c&&c==0)
{cout << "All three cannot be zero!\n";}
elseif ( a+b<=c || b+c<=a || a+c<=b)
{cout << "Not a triangle! Try again!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a != b != c)
{cout << "This is a general triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a==b || b==c || a==c)
{cout << "This is an isosceles triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a==b&&b==c&&c==a)
{cout << "This is an equilteral triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
cout << "Do you want to try more? (yes/no): \n";
cin >> again;
if (again == 'n')
n = true;
}while(!n);
cout << "Goodbye!\n";
system ("pause");
}
but now i cant get an equilateral triangle. if i put all 3 sides the same, it still calls it a general triangle!
#include <iostream>
usingnamespace std;
int main ()
{
char again, n;
do {
double a=0, b=0, c=0;
cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
cout << "What is side a: \n";
cin >> a;
cout << "What is side b: \n";
cin >> b;
cout << "What is side c: \n";
cin >> c;
if ( a==b&&b==c&&c==0)
{cout << "All three cannot be zero!\n";}
elseif ( a+b<=c || b+c<=a || a+c<=b)
{cout << "Not a triangle! Try again!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a != b && a != c && b !=c)
{cout << "This is a general triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a==b&&b==c&&c==a)
{cout << "This is an equilteral triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
elseif (a==b || b==c || a==c)
{cout << "This is an isosceles triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
cout << "Do you want to try more? (yes/no): \n";
cin >> again;
if (again == 'n')
n = true;
}while(!n);
cout << "Goodbye!\n";
system ("pause");
}
OMMGGGG KRAHL! thank you so much. sorry ! i didnt see the last comment about line 21. YAAAY! :D THIS project took so long >.<
OHH! one last question: how would i terminate the program if they all equaled 0? (in the first if)
This line: elseif (a != b != c)
is not what you would expect it to be...(I can't think of an easy way off the top of my head to explain what this means).
It should be elseif (a != b && a != c && b != c)
Also, you should probably put the check for an equilateral triangle above the one for an isosceles triangle, or change the condition for the isosceles triangle to make sure the three sides aren't equal (because if they are, then it's equilateral).
(Easier to just move the check for an equilateral triangle above the one for an isosceles one)
Oh, and just so you understand, the reason your last else (in your previous code) wasn't working was because your code was the same as this:
1 2 3 4 5 6 7 8 9
else
{
(a==b && b==c && c==a); // This statement does nothing
}
{ // This set of brackets doesn't really do anything in this case, so
// this is always displayed
cout << "This is an equilteral triangle!\n";
cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
}
To terminate the program when they all equal 0, you just need this...
1 2 3 4 5
if (a==b && b==c && c==0)
{
cout << "All three cannot be zero!\n";
break; // This gets the program out of your do-while loop, so it prints "Goodbye!"
}