expected primary-expression before else error

I must be missing something and I am completely stuck with this. I am suppose to get the area of a circle, triangle, rectangle, and trapezoid with the user being able to choose which area they want calculated.

Comments

  • thats the code

    //Lab 9 Problem 1
    #include
    #include
    using namespace std;

    int main()
    {
    //variables defined here
    float area, radius, a, b, base, height;
    const double PI = 3.1416;
    char code;
    char exit_variable;

    //data input section
    cout << "The code for the area of a circle is c
    ";
    cout << "The code for the area of a ractangle is r
    ";
    cout << "The code for the area of a triangle is tri
    ";
    cout << "The code for the area of a trapezoid is tra
    ";
    cout << "Enter a specifation code " ;
    cin >> code;

    //if/else function
    if (code == 'c')
    cout << " Enter in a value for radius " ;
    cin >> radius;
    exit_variable;
    area = pow(radius,2)*PI;
    cout << "
    The area is " ;
    cin >> area ;
    else if (code == 'r')
    cout << " Enter in a value for the base " ;
    cin >> base ;
    cout << " Enter in a value for the height " ;
    cin >> height ;
    area = base * height;
    cout << "
    The area is " ;
    cin >> area ;
    else if (code == 'tri')
    cout << " Enter in a value for the base " ;
    cin >> base ;
    cout << " Enter in a value for the height " ;
    cin >> height
    area = (1/2) * base * height
    cout << "
    The area is " ;
    cin >> area ;
    else if (code == 'tra')
    cout << " Enter in a value for the height " ;
    cin >> height ;
    cout << " Enter in a value for the first base " ;
    cin >> a ;
    cout << " Enter in a value for second base " ;
    cin >> b ;
    area = height * ((a+b)/2) ;
    cout << "
    The area is " ;
    cin >> area ;

    //data output section
    cout << "Press the letter e to exit";
    cin >> exit_variable;
    //thats the end
    }
  • oh god the horror...

    [code]
    if ( something )
    do something
    do something else
    [/code]

    is not the same as

    [code]
    if ( something )
    {
    do something
    do something else
    }
    [/code]
    And because of this
    [code]
    if ( something )
    do something
    do something else
    else if ( something else )
    do another thing
    do more stuff
    [/code]
    is illegal. It needs to be:
    [code]
    if ( something )
    {
    do something
    do something else
    }
    else if ( something else )
    {
    do another thing
    do more stuff
    }
    [/code]

    Tip: [b]Always[/b] use braces {}.
  • ya i ended up figuring it out when i had to do it all again with switch. but thanks though. I'm only a college student so I'm not that good at this.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories