simple program using pythagorean theorum

using: Dev-C++
OS: WinXP 32bit

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
39
40
41
42
43
44
45
//A simple interactive program that computes the missing length of a 
//right triangle using the Pythagorean Theorum

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    char k = 'y';
    
        do
    {
          cout << "\n";
          cout << " Hello.\n";
          cout << " This is a simple program that will measure the missing length of a side of a\n" 
                  " right triangle, given the length of the other two sides.\n";
          cout << "\n" "  Are you missing one of the [l]egs, or the [h]ypotenuse\?\n" 
          " (Enter l or h and press ENTER)";
          //put 'cin >> o' and if statements here later, for now assume that user chooses hypo
          //so all this below would be an if(o = h) {block}, than there would be another if(o = l)
          //block later for a missing leg 
          
          double x, y, z;
          z = sqrt( x*x + y*y );
          
          cout << "    Please enter the legth of leg 1: \n ";
          cin >> x;
        
          cout << "    Please enter the length of leg2: \n ";
          cin >> y;
        
          cout << "    The hypotenuse's length is: \n ";
          cout << z;
          cout << " \n\n";
          cin.get();
        
                        cout << "   If  you want to do another triangle, enter [y] and click ENTER.\n";
                        cout << "   To close the program, enter [n] and click ENTER.\n";
                        cout << "\n\n   ";
                        cin >> k;
     } 
    while(k == 'y');
}

So here's the output:

...bla bla bla...
Please enter the length of leg1:
4
Please enter the length of leg2:
5
The hypotenuse's length is:
1.#INF
...bla bla bla...

I am completely honest when I say that I have no idea what 1.#INF means (all real numbers? something along the lines?)
so I tried simplifying it into another, simpler program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double x, y, z;
    
    z = sqrt(x*x + y*y);
    
    cout <<" Enter leg1's length: \n";
    cin >> x;
    
    cout <<" Enter leg2's length: \n";
    cin >> y;
    
    cout << "The hypotenuse is: \n";
    cout << z;
    cout << "\n" "Alrighty then.";
    cin.get()
}

It all worked, but the output said that the hypo was 0... help please?
PS: The if block for the leg would contain 'a = sqrt(b*b-c*c)' where a is the missing leg,
and c would be the hypo, right?
PSS: Am I using if() correctly? (or should I use bool? Sorry for sounding like a newb.)
PSSS: Excuse my horrible indentation.
The problem is that you don't init x and y before call sqrt().

#INF normaly means an infinity number.

I don't see if block in your code
What Eric said, place your code for z after the cin >> y
Thank you guys so much that really worked :)

oh and by the way Eric, I mentioned in the comments how i would later put the code below the first four couts in an if(o=h) block, and then do another block for a missing leg (if(o=l)), but I've decided to use a switch (with an 'h' and 'l' case) instead, because it seems more practical.
Topic archived. No new replies allowed.