Beginner C/C++

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

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
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.
Attachment: lab9 q1.cpp (1646 Bytes | downloaded 41 times)
Report
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 " ;
cin >> radius;
exit_variable;
area = pow(radius,2)*PI;
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
}
Report
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 {}.
Report
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
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.