Pythagorean

Hello Forum,

I'm trying to write a 'simple' program that attempts to sort through the data the user has and outputs the missing variable in the pythagorean formula. I think I have the switch statements handled, assuming this is a good method and I'd be happy to learn better ones, but can't get the math to come out right. I think it has something to do with how I'm trying to square the value but I have no idea. Any advice will greatly be appreciated.

Best Regards,

Xeon

int main(void)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
    using std::cout; 
    using std::cin; 
    using std::string; 
    
    int casescenario; 
    double a;
    double b;
    double c;
   
    cout << "Please examine the following options carefully\n"
         << " 1. You have both a and b value and wish to solve for c\n"
         << " 2. You have both a and c values and wish to solve for b\n"
         << " 3. You have both c and b values and wish to solve for a\n";
    
    cin >> casescenario; 
    
    switch (casescenario)
    { 
           case 1: 
                cout << "Please enter value for a\n";
                cin >> a;
                cout << "Please enter value for b\n"; 
                cin >> b;
                cout << (a*a) + (b*b) << " is the value for C\n";
           case 2:
                cout << "Please enter value for a\n"; 
                cin >> a; 
                cout << "Please enter value for c\n";
                cin >> c; 
                cout << (c*c)-(a*a) << "is the value for b!\n"; 
                
}

system ("pause");
return 0; 

}


P.S.

The case scenario for option three obviously isn't completed yet but I felt like I didn't want to move onto it until I had solved the problems in case two; it seemed to me that they would repeat themselves.
Last edited on
Pythagorean is a*a + b*b = c*c

To Solve for c:

c = sqrt( a*a + b*b )

In your code you have:

c = a*2 + b*2

You have the same problem for when you solve for b.

Also, you need to put a break and the end of your case, or else the program will just start running the next case.

(put a break; between line 25-26, and another one on line 32)


And also solving for a and b would both be pretty much the same, since a and b are interchangable. So scenario 3 is kind of pointless... but whatever.

=)
Just remember that the sqrt(x) function is not defined in the standard C++, it is defined in the cmath library. (#include <cmath>)
Which is part of the C++ standard.
I was mainly referring to the fact that you had to include it manually, I phrased it wrongly, you are indeed correct hellos.
Topic archived. No new replies allowed.