1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

please can someone correct this code

Discussion in 'C++' started by Gaylle, Mar 24, 2009.

  1. Gaylle

    Gaylle New Member

    Joined:
    Mar 24, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    my answers are wrong. dont know what is wrong with the code though there are no build errors.


    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        //declarations
        int operation;
        double CirRadius, CirArea, Base, Height, TriArea;
        const double PI = 3.14159;
    
        // menu
        cin >> operation;
        while (operation !=4)
        {
            if (operation == 1)
            {
                cin >> CirRadius;
                  CirArea = PI * CirRadius * CirRadius;
                cout<<"The area of a circle with radius "<<CirRadius<<" is " <<setprecision(4)<<CirArea<<'\n\n';
                cin >> operation;
            }
            else if (operation == 2 || operation == 3)
            {
                cin >> Base;
                cin >> Height;
                TriArea = 0.5 * Base * Height;
                cout<<"The area of a triangle with base "<<Base<<" and height "<<Height<<" is "<<TriArea<<'\n\n';
                cin >> operation;
            }
            else if (operation == 5 || operation == 7)
            {
                cout<<"Error: "<<operation<<" is not a valid operation.\n\n";
    
            }
            
        }
    
        return 0;
    }
    
     
    Last edited by a moderator: Mar 31, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Use code tags when posting code.
    In what way are the answers wrong? What data did you enter, what results did you expect, and what results did you get?

    From your other post I guess it's probably something to do with this line:
    Code:
    else if (operation == 2 || operation == 3)
    
    2 is a rectangle, isn't it?



    Hmm.
    Dammit, another duplicate.
    http://www.go4expert.com/showthread.php?t=16651

    I'm going to stop wasting my time on solving the problems you've already solved but can't be arsed to tell us you've solved them.
     
  3. Gaylle

    Gaylle New Member

    Joined:
    Mar 24, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    i hadnt solved the problem like u think. i'm just doin it now. am grateful you pointed out my mistake tho. tnx
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Where are you up to? Could you (restricting posts on this code to this thread only) post the code you've got so far, with code tags, and explain what the problem is, what input you gave if you're getting wrong results, what output you expected and what output you got.
     

Share This Page