I have written a program using the Pythagorean theorem to figure out if the triangle is a right triangle or not. We are supposed to have it set up so that no matter which number is entered first the program knows which number is the hypotenuse. I have it written, and it is compiling, but, for some reason it is requesting that each number be entered twice. And, I can't get it to give a yes to whether it is a right triangle. Any suggestions or help would be greatly appreciated.
Crazy but I did figure some of it our. It is partially working. Just needs a little more refinement on the sorting and to figure out why you have to input the side lengths twice but, it works and does a little sorting.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
double num1,num2,num3;
//triangle side lengths request section;
cout << "Please enter the first side of your triangle's length:" <<endl;
cin >> num1;
cout<<endl;
{
if(num1<0)
cout<<"Please enter a positive number:"<< endl;
cin>>num1;
cout<<endl;
}
cout << "Please enter the second side of your triangle's length:"<< endl;
cin >> num2;
cout<<endl;
{
if(num2<0)
cout<<"Please enter a positive number:"<< endl;
cin>> num2;
cout<<endl;
}
cout << "Please enter the third side of your triangle's length:"<< endl;
cin >>num3;
cout<<endl;
{
if(num3<0)
cout<<"Please enter a positive number:"<< endl;
cin>>num3;
cout<<endl;
}
double a,b,c; //Side length sorting section;
{
if(num1<num2)
a=num1;
cout<<endl;
if(num2<num1)
a=num2;
cout<<endl;
}
{
if(num2<num3)
b=num2;
else
b=num3;
cout<<endl;
}
{
if(num3>num2&&num3>num2)
c=num3;
else
c=num1;
cout<<endl;
}
{
if((pow(a,2))+(pow(b,2))== (pow(c,2)))
cout<< "Your triangle is a right triangle!"<<endl;
else
cout<< "I'm sorry, your triangle is not a right triangle."<<endl;
}
return 0;
}
#include <iostream>
#include <iomanip>
#include <cmath>
double read_side( constchar* tag )
{
std::cout << "Please enter the " << tag << " side: " ;
double d ;
if( std::cin >> d && d > 0 ) return d ;
// handle error
// http://www.cplusplus.com/reference/ios/ios/clear/
std::cin.clear() ; // clear input error state
// http://www.cplusplus.com/reference/istream/istream/ignore/
std::cin.ignore( 1000, '\n' ) ; // throw away any non-numeric input
std::cerr << "Please enter a positive number\n" ;
// and try again
return read_side(tag) ;
}
int main()
{
// floating point computations are approximate
// if the absolute difference between x and y is less than the tolerance,
// we treat x and y as equal
constdouble tolerance = 0.0001 ;
// read the three sides
double a = read_side( "first" ) ;
double b = read_side( "second" ) ;
double c = read_side( "third" ) ;
// make a the largest of the three
// http://www.cplusplus.com/reference/algorithm/swap/if( a < b ) std::swap(a,b) ;
if( a < c ) std::swap(a,c) ;
std::cout << std::fixed << "\nthe largest side is " << a
<< " and the other two sides are " << b << " and " << c << '\n' ;
// is it a triangle?
if( a > (b+c) ) std::cerr << "These sides do not form a triangle\n" ;
// is it right-=angled?
// http://www.cplusplus.com/reference/cmath/abs/elseif( std::abs( a*a - ( b*b + c*c ) ) < tolerance )
std::cout << "Your triangle is a right triangle!\n" ;
else
std::cout << "I'm sorry, your triangle is not a right triangle.\n" ;
}
That is beautiful but, unfortunately we have not been introduced to functions yet. Through my own research I have seen them used but, the class is basically up to control loops. So, I am limited. But, thank you so much for your input. That is really nice. I will copy that so I have a reference to look at while I am learning to work with functions. Sometimes, for me anyway, a quick reference can make all the difference in my making the connection of how I can use the concept in my own program build.
@GRex2595
Actually, only lines 3 and 4 will ever be executed (regardless of what x is), since if (x = 0)
first assigns the value of 0 to x, and then compares whether x (which now has a value of 0) evaluates to true (nonzero), which of course it doesn't.
You probably meant if (x == 0) // Two equals signs .