Thread: C++ Program, please help!

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    22

    C++ Program, please help!

    Hey guys, I was wondering if you could help me out on an assignment I have to do for my introductory programming class.

    Here is the link to the assignment:

    http://www.cs.uwm.edu/classes/cs201/.../04/04/04.html


    And here is my code so far:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    
    //Declare variables.
    float A;
    float B;
    float C;
    
    //Get lengths of triangle.
    cout << "Please enter the triangle lengths:" << endl;
    cout << "length 1: ";
    cin >> A;
    cout << "length 2: ";
    cin >> B;
    cout << "length 3: ";
    cin >> C;
    
    //Order the lengths.
    
    if (A>B)
    {
    float temp;
    temp = A;
    A = B;
    B = temp;
    }
    
    
    if (A>C)
    {
    float temp;
    temp = A;
    A = C;
    C = temp;
    }
    
    if (B>C)
    {
    float temp;
    temp = B;
    B = C;
    C = temp;
    }
    
    //Display order.
    
    cout << "Lengths in nondecreasing order: "<< A << "," << B << ","
    << C <<endl;
    
    return 0;
    
    }
    Everything seems right so far...but I'm stuck as where to go from here. I have a lot of trouble with functions!

    Any suggestions would be appreciated!

    Thanks in advance!

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    exactly what trouble are you having? no-one is going to do this assignment for you, but we're happy to help you along the way

    to get you started on the functions. you need to declare them first
    Code:
    #include <iostream>
    using namespace std;
    
    bool isTriangle(float x, float y, float z);
    bool isIsoceles(float x, float y, float z);
    bool isEquilateral(float x, float y, float z);
    bool isScalene(float x, float y, float z);
    bool isAcute(float x, float y, float z);
    bool isRight(float x, float y, float z);
    bool isObtuse(float x, float y, float z);
    
    int main()
    {
    // code etc
    then define them after main
    Code:
    } // end of main
    
    bool isTriangle(float x, float y, float z)
    {
        // figure out if it's a triangle and 
        return true; // or false depending on answer
    }
    you can then call isTriangle() in main()
    Code:
    int main()
    {
       // snip
       //Display order.
    
        cout << "Lengths in nondecreasing order: "<< A << "," << B << ","
        << C <<endl;
    
        if (isTriangle(A, B, C))
        {
            cout << "it's a triangle";
        }
        else
        {
            cout << "that's no triangle... ";
        }
        cout << endl;
        return 0;
    
    }
    that should get you started
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Thank you for the quick reply...I'll take a look at this!

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Okay, I got this far...but I'm not getting the same answers shown in the sample on the link.

    Sorry if my code is long, I'm just a beginner!

    Any suggestions?

    Thanks in advance!



    Code:
    #include <iostream>
    using namespace std;
    
    
    
    //Function Prototypes.
    bool isTriangle(float x, float y, float z);
    bool isIsoceles(float x, float y, float z);
    bool isEquilateral(float x, float y, float z);
    bool isScalene(float x, float y, float z);
    bool isAcute(float x, float y, float z);
    bool isRight(float x, float y, float z);
    bool isObtuse(float x, float y, float z);
    
    
    
    //Main Function
    int main ()
    {
    
    
    
    //Declare variables.
    float A;
    float B;
    float C;
    
    
    
    //Get lengths of triangle.
    cout << "Please enter the triangle lengths:" << endl;
    cout << "length 1: ";
    cin >> A;
    cout << "length 2: ";
    cin >> B;
    cout << "length 3: ";
    cin >> C;
    
    
    
    //Order the lengths.
    if (A>B)
    {
    float temp;
    temp = A;
    A = B;
    B = temp;
    }
    
    if (A>C)
    {
    float temp;
    temp = A;
    A = C;
    C = temp;
    }
    
    if (B>C)
    {
    float temp;
    temp = B;
    B = C;
    C = temp;
    }
    
    
    //Display order.
    cout << "Lengths in nondecreasing order: "<< A << "," << B << ","
    << C <<endl;
    
    
    
    //Function calls and final output.
    cout<<"The triangle is: ";
    
    if(isTriangle(A,B,C))
    {
    if(isIsoceles(A,B,C))
    cout<<"Isoceles"<<endl;
    }
    else if(isEquilateral(A,B,C))
    {
    cout<<"Equilateral"<<endl;
    }
    else if(isScalene(A,B,C))
    {
    cout<<"Scalene"<<endl;
    }
    else if(isAcute(A,B,C))
    {
    cout<<"Acute"<<endl;
    }
    else if(isRight(A,B,C))
    {
    cout<<"Right"<<endl;
    }
    else if(isObtuse(A,B,C))
    {
    cout<<"Obtuse"<<endl;
    }
    else
    {
    cout<<"Illegal"<<endl;
    
    
    return 0;
    }}
    
    
    //Function Definitions.
    bool isTriangle(float x, float y, float z)
    {
    if (x>0 && (x + y >z))
    {
    return true;
    }
    else
    {
    return false;
    }}
    
    bool isIsoceles(float x, float y, float z)
    {
    if ((x=y) || (y=z))
    {
    return true;
    }
    else
    {
    return false;
    }}
    
    bool isEquilateral(float x, float y, float z)
    {
    if ((x=y) && (y=z))
    {
    return true;
    }
    else
    {
    return false;
    }}
    
    bool isScalene(float x, float y, float z)
    {
    if (x<y && (y<z))
    {
    return true;
    }
    else
    {
    return false;
    }}
    
    bool isObtuse(float x, float y, float z)
    {
    if (((x*x)+(y*y))<(z*z))
    {
    return true;
    }
    else
    {
    return false;
    }}
    
    bool isRight(float x, float y, float z)
    {
    if (((x*x)+(y*y))==(z*z))
    {
    return true;
    }
    else
    {
    return false;
    }}
    
    bool isAcute(float x, float y, float z)
    {
    if (((x*x)+(y*y))>(z*z))
    {
    return true;
    }
    else
    {
    return false;
    }}

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Did you know that all Equilateral triangles are Isoceles triangles? So what are the outputs to the inputs of an Equilateral triangle when you say this?

    Code:
    if(isIsoceles(A,B,C))
    cout<<"Isoceles"<<endl;    // You had a } after this but no { before it
    else if(isEquilateral(A,B,C))
    {
    cout<<"Equilateral"<<endl;
    }
    By the way you had an extra } after this statement which is screwing with your output. You would see those things if you actually indented.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The format of your code is terrible. Everything should be indented properly, e.g.
    Code:
    int num = 10;
    if(num == 10)
    {
    	cout<<num<<endl;
    }
    else
    {
    	cout<<"no"<<endl;
    }
    -----

    Code:
    //Declare variables.
    float A;
    float B;
    float C;
    Always initialize your variables. If you don't know what that means, look it up.
    ---

    Code:
    nondecreasing order
    What the heck does "nondecreasing" mean? "Increasing"?

    The basic strategy for writing a program is to write ONE function. Then, you test that function in main(). Once everything is working correctly, you move on and write another function. What you don't want to do is write 7 functions and then click on the compile button. So, start a new program and cut and paste your first function into your new program, and test it until it works. Continue on that way until you get all 7 functions to work.

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you should also design your fuctions so that each function call is as complete as possible. In your case this means that in isIsoceles you should call isTriangle. This prevents error. we don't like error
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Eh, I'm still clueless as to what to do....

    7stud, sorry my code is sloppy, I'm using putty to write the code for my class and it doesn't automatically indent.

    I think there is an issue with my "if" statements when calling the functions, because I keep getting an answer of "Equilateral" when the inputs I enter should come up with "Illegal".

    Any suggestions?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM