Problem compiling and getting "expected primary-expression before "else" error
Author |
Message |
Creatlv3
|
Posted: Wed Feb 10, 2010 4:03 am Post subject: Problem compiling and getting "expected primary-expression before "else" error |
|
|
I am writing a program for my class and it needs to incorporate the cin.fail() command which i have and i believe it works fine, the problem arises with my if/else statements. the code is as follows
code: | #include <iostream>
#include <cmath>
using namespace std;
int main()
{
/*
a=volume
b=surface area
r=radius
*/
double x;
double r;
double a;
double b;
double c;
double h;
double l;
double w;
cout<< "What shape would you like?"<<endl;
cout<< "1) Sphere"<< endl;
cout<< "2) Cone"<< endl;
cout<< "3) Rectangular Prism"<< endl;
cout<< "4) Cylinder"<< endl;
cin>>x;
if (x==1) // Sphere
{
cout<<"Radius=";
cin>>r;
while(cin.fail()<'0');
{
cout << "ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
cin.clear();
cin.ignore(256,'\n');
cout<< "Radius=";
cin>>r;
}
a=((4.0/3.0)*M_PI)*pow(r,3.0);
b=(4*M_PI)*pow(r,2.0);
cout<<"Volume="<<a<< endl;
cout<<"Surface Area="<<b<<endl;
}
else if (x==2) // Cone [b]Line 46[/b]
{
cout<<"Radius="<<endl;
cin>>r;
while (cin.fail()<'0');
{
cout<<"ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
cin.clear();
cin.ignore(256,'\n');
cout<< "Radius="<<endl;
cin>>r;
}
cout<<"Height="<<endl;
cin>>h;
while (cin.fail()<'0');
{
cout<<"ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
cin.clear();
cin.ignore(256,'\n');
cout<< "Radius="<<endl;
cin>>h;
}
c=(M_PI*pow(r,2.0));
a=(((1.0/3.0)*M_PI)*pow(r,2.0))*h;
b=((M_PI*r)*(sqrt(pow(r,2.0)+pow(h,2.0))+r));
cout<<"Volume="<<a<<endl;
cout<<"Surface Area="<<b<<endl;
return 0;
}
else if (x==3) // Rectangular Prism [b]Line 77[/b]
{
cout<<"Length="<<endl;
cin>>l;
while (cin.fail()<0);
{
cout<<"ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
cin.clear();
cin.ignore(256,'\n');
cout<< "Radius="<<endl;
cin>>l;
}
cout<<"Width="<<endl;
cin>>w;
while (cin.fail()<0);
{
cout<<"ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
cin.clear();
cin.ignore(256,'\n');
cout<< "Radius="<<endl;
cin>>w;
}
cout<<"Height=";
cin>>h;
while (cin.fail()<0);
{
cout<<"ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
cin.clear();
cin.ignore(256,'\n');
cout<< "Radius="<<endl;
cin>>h;
}
a=l*w*h;
b=(2*l*w)+(2*l*h)+(2*w*h);
cout<<"Volume="<<a<<endl;
cout<<"Surface Area="<<b<<endl;
return 0;
}
else (x==4) // Cylinder [b]Line 118[/b]
{
cout<<"Radius="<<endl;
cin>>r;
while (cin.fail()<0);
{
cout<<"ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
cin.clear();
cin.ignore(256,'\n');
cout<<"Radius="<<endl;
cin>>r;
}
cout<<"Height=";
cin>>h;
while (cin.fail()<0);
{
cout<<"ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
cin.clear();
cin.ignore(256,'\n');
cout<<"Height=";
cin>>r;
}
a=(M_PI*pow(r,2.0))*h;
b=(2*(M_PI*pow(r,2.0)))+(2*M_PI*r*h);
cout<<"Volume="<<a<<endl;
cout<<"Surface Area="<<b<<endl;
return 0;
}
} |
the error message is as follows
code: | area.C:46 error: expected primary-expression before "else"
area.C:46 error: expected `;' before "else"
area.C:77 error: expected primary-expression before "else"
area.C:77 error: expected `;' before "else"
area.C:118 error: expected primary-expression before "else"
area.C:118 error: expected `;' before "else" |
I just started taking this class 2 weeks ago and i feel confident about the program being able to work if i can get those else statements to work.
Any help would be appreciated
Dave |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Wed Feb 10, 2010 4:11 am Post subject: RE:Problem compiling and getting "expected primary-expression before "else" error |
|
|
It might be that you have semicolons after every while statement.
If it's not a problem with the line itself, it's bound to be some lame extra ; or } added/missed somewhere leaving your one level up or down. |
|
|
|
|
|
Creatlv3
|
Posted: Wed Feb 10, 2010 4:14 am Post subject: Re: Problem compiling and getting "expected primary-expression before "else" error |
|
|
yea i solved that problem and was in the middle of re editing the problem i am having, it was a case of too many ;'s
now my problem is that my while loops are messing up and not proceeding like they should, any suggestions? |
|
|
|
|
|
Zren
|
Posted: Wed Feb 10, 2010 4:36 am Post subject: RE:Problem compiling and getting "expected primary-expression before "else" error |
|
|
Personally I'd do a do-while loop since the logic needs to run through the code first before needing an error checking.
I'm not that adept in C++ really, but from what I can see of cin.fail(), it seems to return a boolean?
http://msdn.microsoft.com/en-us/library/71t65ya2%28VS.80%29.aspx
Don't do the method in that article though, just read cin.fail() description.
So I'm not sure why your doing:
while (cin.fail()<0)
and
while (cin.fail()<'0')
So instead try something along the lines of
code: |
do
[get input]
exit conditions are: if isNumber AND input>0
|
|
|
|
|
|
|
wtd
|
Posted: Thu Feb 11, 2010 12:01 am Post subject: RE:Problem compiling and getting "expected primary-expression before "else" error |
|
|
Does this compile?
code: | int main()
{
if (true)
{
}
else (1)
{
}
}
|
|
|
|
|
|
|
DemonWasp
|
Posted: Thu Feb 11, 2010 12:24 am Post subject: RE:Problem compiling and getting "expected primary-expression before "else" error |
|
|
The problem is that you have:
code: |
} else ( x == 4 ) {
|
which isn't valid. If you want to check the condition, use else if. If you don't want to check the condition, remove it. If you want to make the (expected) condition for that block obvious but you also want it to be a simple else, use a comment. |
|
|
|
|
|
|
|