## Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of posts: 16949

This Forum Only

expected primary-expression before else error Posted by bueno77 on 24 Mar 2011 at 8:55 AM
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.
Re: expected primary-expression before else error Posted by bueno77 on 24 Mar 2011 at 9:52 AM
thats the code

//Lab 9 Problem 1
#include<iostream>
#include<cmath>
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\n";
cout << "The code for the area of a ractangle is r\n";
cout << "The code for the area of a triangle is tri\n";
cout << "The code for the area of a trapezoid is tra\n";
cout << "Enter a specifation code " ;
cin >> code;

//if/else function
if (code == 'c')
cout << " Enter in a value for radius " ;
exit_variable;
cout << "\n 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 << "\n 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 << "\n 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 << "\n The area is " ;
cin >> area ;

//data output section
cout << "Press the letter e to exit";
cin >> exit_variable;
//thats the end
}
Re: expected primary-expression before else error Posted by nebgast on 24 Mar 2011 at 5:07 PM
oh god the horror...

```if ( something )
do something
do something else
```

is not the same as

```if ( something )
{
do something
do something else
}
```

And because of this
```if ( something )
do something
do something else
else if ( something else )
do another thing
do more stuff
```

is illegal. It needs to be:
```if ( something )
{
do something
do something else
}
else if ( something else )
{
do another thing
do more stuff
}
```

Tip: Always use braces {}.
Re: expected primary-expression before else error Posted by bueno77 on 24 Mar 2011 at 6:17 PM
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.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic