# Pythagorean Theorem Program Comment, criticize, or whatever

• 02-11-2009
Sshakey6791
Pythagorean Theorem Program Comment, criticize, or whatever
Code:

```#include <iostream> #include <cmath> #include <string> int main() {         double SideA, SideB, SideC, Hyp; // Hyp is the Hypotenuse         std::string GetInfo;         std::cout << "The Pythagorean Theorem \n\n";                 std::cout << "Do you know the hypotenuse? \n>";         std::cin >> GetInfo;         std::cout << std::endl;         std::cin.sync();                 if ( GetInfo == "no" ) {                 std::cout << "Enter Your Sides: \n";                 std::cout << "1st: ";                 std::cin >> SideA;                 std::cout << "2nd: ";                 std::cin >> SideB;                 std::cin.ignore();                 Hyp = sqrt( pow( SideA, 2) + pow( SideB, 2));                 std::cout << "Hypotenuse = " << Hyp << std::endl;         } else if ( GetInfo == "yes" ) {                 std::cout << "Enter Your Data: \n";                 std::cout << "Hypotenuse: ";                 std::cin >> SideC;                 std::cout << "Side: ";                 std::cin >> SideA;                 std::cin.ignore();                 if ( SideC < SideA )                 {                         std::cerr << "ERROR: The sides can't be bigger then the hypotenuse. \n";                         return EXIT_FAILURE;                 }                 SideB = sqrt( pow( SideC, 2) - pow( SideA, 2) );                 std::cout << "Missing side = " << SideB << std::endl;         } else if ( GetInfo != "no" || GetInfo != "yes" ) {                 std::cout << "ERROR: Case Sensitive or Spelling \n";                 return EXIT_FAILURE;         }         std::cin.get();         return(0); }```
Any advice would be appreciated and I thank you in advance.
• 02-11-2009
Foet
Code:

`else if ( GetInfo != "no" || GetInfo != "yes" )`
I don't think this condition matters, but it should be && instead of ||. The way you have it, it will always evaluate to true.
• 02-11-2009
QuestionKing
You could get by with SideA, SideB, and Hyp I would think, omitting SideC.
I probably would have organized my 3 sides within a structure.
Also I noticed you have included <string> you can swap all input to all uppercase or all lowercase to omit the caps problems
• 02-12-2009
grumpy
pow(x, 2) would be more simply expressed as x*x.

Also, one trouble with computing sqrt(x*x + y*y) is that it will overflow for large x and/or y, even if the result (after computing the square root) can be represented in a double.

One common trick to get around this would be;
Code:

```// your approach Hyp = sqrt( pow( SideA, 2) + pow( SideB, 2));    // blech!!! // slightly better Hyp = sqrt(SideA*SideA + SideB*SideB);    //  can overflow for big values of SideA or SideB // better approach if you want to reduce chances of overflow double temp; if (sideA > sideB) {   temp = sideB/sideA;                            // guaranteed to produce a result < 1     Hyp = sideA * sqrt(1.0 + temp*temp); //  1.0 + temp*temp is guaranteed < 2 } else {     temp = sideA/sideB;                            // guaranteed to produce a result <= 1     Hyp = sideB * sqrt(1.0 + temp*temp); //  1.0 + temp*temp is guaranteed <= 2 }```
• 02-12-2009
iMalc
Quote:

Originally Posted by grumpy
Also, one trouble with computing sqrt(x*x + y*y) is that it will overflow for large x and/or y, even if the result (after computing the square root) can be represented in a double.

The answer to that problem is _hypot. Of course that defeats the purpose of what the OP is trying to learn here though.
• 02-12-2009
Sshakey6791
?
Quote:

Originally Posted by QuestionKing
Also I noticed you have included <string> you can swap all input to all uppercase or all lowercase to omit the caps problems

Can you show me how to do that ? or send me a example? help would be appreciated.

Thanks.
• 02-12-2009
QuestionKing
Here is a tutorial on this site for converting strings to upper case.
• 02-13-2009
grumpy
Quote:

Originally Posted by iMalc
The answer to that problem is _hypot. Of course that defeats the purpose of what the OP is trying to learn here though.

Which is why I didn't bother to mention it .....