HELP!!!!!!!

Hello

OK so i am writing this program for the pythagorean Theorem and i got everything together and went to compile it, but i keep many errors and i cant figure out what im doing wrong
Here is the program:


#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
int leg1;
int leg2;
int H;
int ans;
int choice;

cout<< "Pick option 1 or 2: 1. Leg is missing 2.Hypotenuse is missing"<< endl;
cin>> choice >> endl;

switch(choice)
{
Case 1:
cout<< "Enter values of leg and hypotenuse:";
cin >> leg1 >> H >> endl;

(H*H) - (leg1*leg1) = ans;
ans = sqrt(ans);

cout << "The missing leg is: " <<ans;
break;


Case 2:

cout << "Enter value of both legs: ";
cin>> leg1 >> leg2 >> endl;


ans = (leg1*leg1) + (leg2*leg2);
ans = sqrt(ans);

cout << "The hypotenuse is " << ans;
break;

default:
cout <<"Must enter a 1 or 2";
}

system("Pause")
return 0;
}



PLEASE HELP!!!!! Thank you
Please use code tags when posting code - use the <> button to the right of the textbox.

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
46
47
48
49
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
int leg1;
int leg2;
int H;
int ans;
int choice;

cout<< "Pick option 1 or 2: 1. Leg is missing 2.Hypotenuse is missing"<< endl;
cin>> choice >> endl;

switch(choice)
{
Case 1:
cout<< "Enter values of leg and hypotenuse:";
cin >> leg1 >> H >> endl;

(H*H) - (leg1*leg1) = ans;
ans = sqrt(ans);

cout << "The missing leg is: " <<ans;
break;


Case 2:

cout << "Enter value of both legs: ";
cin>> leg1 >> leg2 >> endl;


ans = (leg1*leg1) + (leg2*leg2);
ans = sqrt(ans);

cout << "The hypotenuse is " << ans;
break;

default:
cout <<"Must enter a 1 or 2";
}

system("Pause")
return 0;
}


Line 24 should be written as ans = (H*H) - (leg1*leg1);.

Lines 20 and 31: Keywords in C++ are case-sensitive. Use case.

You're missing a semicolon on line 47.

Don't use endl with cin. (Lines 16, 22, and 34)
Last edited on
Thank you so much i was so annoyed with that program
I had another question about it though here is it a little improved


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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
    int leg1;
    int leg2;
    int H;
    int ans;
    int choice;
    int y=1;
    int n=0;
    char response;

    do
{
    cout<< "Pick option 1 or 2:" << endl << "1. Leg is missing 2.Hypotenuse is missing"<< endl;
    cin>> choice;

    switch(choice)
    {
        case 1:
        cout<< "Enter values of leg:";
        cin >> leg1;
        cout << "Enter value of hypotenuse:";
        cin >> H;

        ans = (H*H) - (leg1*leg1);
        ans = sqrt(ans);

        cout << "The missing leg is: " << ans << endl;
        break;


        case 2:

        cout << "Enter value of one leg: " << endl;
        cin>> leg1;
        cout << "Enter value of other leg:" << endl;
        cin >> leg2;

       ans = (leg1*leg1) + (leg2*leg2);
       ans = sqrt(ans);

       cout << "The hypotenuse is " << ans << endl;
       break;

       default:
       cout <<"Must enter a 1 or 2";

    }

    cout << "Would you like to try for another right triangle? (y/n)" << endl;
    cin >> response;
    system("CLS");
}

    while (response == 'y');


    system("Pause");
    return 0;
}


When i run, it in the beginning when i dont press a one or two it goes to default and then it will also run the want to try again part. How do i get it to not run the try again part when it goes to default but instead run one that says something like retry because i want them to press a one or two
Works fine for me. Only thing I see wrong now is that you declare int y and n but don't use them. Otherwise it compiled fine and ran using Code::Blocks and GCC compiler 4.7.2
well what i want to happen is that i want the try for another triangle to apply to only case1 and 2 but not happen at default. At default i want it to just retart
It might be easier to check if the user input was acceptable up after line 21 with a while loop instead of checking in the switch statement.

1
2
3
4
5
cin >> choice;
while(choice != 1 && choice != 2) {
    cout << "You must enter a 1 or 2" << endl;
    cin >> choice;
}
As an aside, pls clean-up your var name list.

"H" does not tell me anything at all about what this var is, what it is used for, where it is used, nor what it's TYPE is.

INT i_hypo // (user supplied int val for hypotenuse)

The same is TRUE for x,y,z,n,y
If you need a BOOL YES then define it at the beginning of the app with something more like this:

#define YES 1 // no semicolon here
#define NO 0 // no semicolon here

Vars like ans,choice etc, come very close to reserved words and may, as well, become confusing.
int i_usr_input;
int i_usr_choice;
int i_usr_ansr;

Too, place your code into subrt'ns for easier development and debugging.

While these suggestion may not make your code execute any faster, they will have you hours trying to debug it later.

Last edited on
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double a, b, c;
char choice=-1;
while(choice!='3'){

cout<<"Pick option:"<< endl
<<"\t1. Leg is missing"<< endl
<<"\t2.Hypotenuse is missing"<< endl
<<"\t3.Exit"<< endl;
cin>>choice;

switch(choice){

case '1':

cout<< "Enter values of leg: ";
cin >> a;
cout<<endl;

cout<< "Enter values of hepotinuse: ";
cin >> c;
cout<<endl;

b=sqrt( pow(c,2) - pow(a,2) );

cout << "The missing leg is: " <<b<<endl;

break;

case '2':

cout << "Enter value of the one leg: ";
cin>> a;
cout<<endl;

cout << "Enter value of the other leg: ";
cin>> b;
cout<<endl;

c=sqrt( pow(a,2) + pow(b,2) );

cout << "The hypotenuse is " << c << endl;

break;

case '3':

break;

}
}
}
iLL LOOK INTO ALL OF THOSE THANK YOU
Topic archived. No new replies allowed.