Help Please

  • Thread starter Thread starter dudeman
  • Start date Start date
D

dudeman

Guest
Whats wrong with this code, even if I enter 2 it does the first if statment :/
Code:
#include <iostream.h>

int main(void)
{
    for(;;)
    {         
              int choice;
              cout << "Press 1 to find the hypotonuse of a triangle\nPress 2 to find the side of a triangle";
              cout << "\n\nYour Choice:";
              cin >> choice;
              
              if(choice == 1);
              {
                        double side1;
                        double side2;
                        double hypot;
                        cout << "\nEnter side one:";
                        cin >> side1;
                        cout << "Enter side two:";
                        cin >> side2;
                        side1 = side1 * side1;
                        side2 = side2 * side2;
                        hypot = side1 + side2;
                        cout << "\nThe hypotonuse is:" << hypot << "\n\n\n";
              }        
              if(choice == 2);
              {
                        double side1;
                        double side2;
                        double hypot;
                        cout << "\nEnter the hypotonuse:";
                        cin >> hypot;
                        cout << "Enter side:";
                        cin >> side1;
                        side1 = side1 * side1;
                        hypot = hypot * hypot;
                        side2 = hypot / side2;
                        cout << "\nThe other side is:" << side2 << "\n\n\n";
              }
              
    }
    
              
    return 0;
}
 
drop the semicolon after the if statement

In otherwords, what you are saying is "If the choice was 1 then... nothing". Then you have a block of code that it will always execute, then you have a second empty conditional statement followed by a block of code that executes every time.

Also, as general good practice and to help readability, you should usually use "else if" statements on related conditions that should never both occur. In this situation you would never want both operations performed so you would make sure it could never possibly happen by using an else if statement.


Oh yah, and you calculated the hypotenuse wrong. You did side one squared plus side two squared but didnt take the square route.
 
Ya, I know... but how do I find the square root of something?
 
There's a cmath function I believe called sqrt().
 
Yeah, it's to do with the ; thing. And wouldn't it be cleaner to use a "While" loop?

Oh, and the second "if" statement should be an "Else If", because it's prettier that way.
And by the looks of it, you have no clean way of ending the program. Have an "Else If (choice == 0) return 0; or something.

Oh, and it's good programming practice to begin integer names with the letter "i". Like "iChoice".

-Angry Lawyer
 
Ya, the book im reading begins them with n but I didnt bother yet cause its so short, if i was writing alot more code i would.
 
Back
Top