Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Compiler Error - Expected primary expression before else...

jennbojennbo Posts: 3Member
Hello -
I am getting a compiler error message and I'm not sure how to correct my code to resolve the issue. I've posted the entire program code below, but the area I'm having a problem with is the else statement. The compiler points to the line where else is written and gives me the message stating "expected ';' before else"; however I double-checked and haven't left out any semi-colons. I did notice that when I removed the calls to the areaEquilateral and areaTriangle functions from the if/else statement, the program would compile and run although all output values were incorrect and I'm not sure what the difference was. Obviously there's got to be something I'm missing. Any assistance is greatly appreciated!

----------------------

#include
#include
#include
#include

using namespace std;

double areaEquilateral(double, double, double);
double perimeterTriangle(double, double, double);
double areaTriangle(double, double, double);
void printIdInfo(ostream &);
void readData(ifstream &, double &, double &, double &);
void printInvalidMsg();
void printEquilateralMsg(ofstream &);
void printResults(ofstream &);

int main()
{
double side1,
side2,
side3,
eq_area,
area,
p;

ifstream infile;
ofstream outfile;

infile.open("C:\Users\Jennbo\Desktop\prog10_inp.txt");

if (!infile)
{
cout << "Failed to open input file.
";

system("PAUSE");
return 2;
}

outfile.open("C:\Users\Jennbo\Desktop\prog10_out.txt");

if (!outfile)
{
cout << "Failed to open output file.";

system("PAUSE");
return 2;
}

printIdInfo(cout);
printIdInfo(outfile);

readData(infile, side1, side2, side3);

if (side1 <= 0 || side2 <= 0 || side3 <= 0)
{
printInvalidMsg();
}

while (side1 >0 && side2 >0 && side3 >0)
{
{
if (side1 == side2 && side2 == side3)
eq_area = areaEquilateral(side1, side2, side3);
printEquilateralMsg(outfile);
[b]else[/b]
area = areaTriangle(side1, side2, side3);
}
p = perimeterTriangle(side1, side2, side3);
printResults(outfile);
readData(infile, side1, side2, side3);
}

infile.close();
outfile.close();

system("PAUSE>NUL");
return 0;
}

double perimeterTriangle(double side1, double side2, double side3)
{
double p;

p = (side1 + side2 + side3);

return p;
}

double areaEquilateral(double side1, double side2, double side3)
{
double eq_area;

eq_area = (side1 * side1 * sqrt (3) ) / 4;

return eq_area;
}

double areaTriangle(double side1, double side2, double side3)
{
double area,
p;

p = perimeterTriangle (side1, side2, side3) / 2.0;
area = sqrt ( p * ( p - side1 ) * ( p - side2 ) * ( p - side3 ) );

return area;
}

void printIdInfo(ostream & out)
{
out <<"Jennifer Harrow
"
<<"C.S.1428.3
"
<<"11/15/07

";
}

void readData(ifstream & infile, double &side1, double &side2, double &side3)
{
infile >> side1 >> side2 >> side3;
}

void printInvalidMsg()
{
cout << "There were no valid input values on the input file.";
}

void printEquilateralMsg(ofstream & outfile)
{
outfile << "The triangle is an equilateral triangle.
";
}

void printResults(ofstream & outfile)
{
double side1,
side2,
side3,
eq_area,
p;

outfile << "The sides of the triangle are " << setprecision(1)
<< fixed << side1 << ", " << side2 << ", and " << side3
<< ".
"
<< "The area of the triangle is " << eq_area << ".
"
<< "The perimeter of the triangle is " << p << ".

";
}

Comments

  • IDKIDK Posts: 1,784Member
    Please use code tags when posting code.

    Also you should have pointed out the function where it occurred and the whole error message.

    [code]
    : #include
    : #include
    : #include
    : #include
    :
    : using namespace std;
    :
    : double areaEquilateral(double, double, double);
    : double perimeterTriangle(double, double, double);
    : double areaTriangle(double, double, double);
    : void printIdInfo(ostream &);
    : void readData(ifstream &, double &, double &, double &);
    : void printInvalidMsg();
    : void printEquilateralMsg(ofstream &);
    : void printResults(ofstream &);
    :
    : int main()
    : {
    : double side1,
    : side2,
    : side3,
    : eq_area,
    : area,
    : p;
    :
    : ifstream infile;
    : ofstream outfile;
    :
    : infile.open("C:\Users\Jennbo\Desktop\prog10_inp.txt");
    :
    : if (!infile)
    : {
    : cout << "Failed to open input file.
    ";
    :
    : system("PAUSE");
    : return 2;
    : }
    :
    : outfile.open("C:\Users\Jennbo\Desktop\prog10_out.txt");
    :
    : if (!outfile)
    : {
    : cout << "Failed to open output file.";
    :
    : system("PAUSE");
    : return 2;
    : }
    :
    : printIdInfo(cout);
    : printIdInfo(outfile);
    :
    : readData(infile, side1, side2, side3);
    :
    : if (side1 <= 0 || side2 <= 0 || side3 <= 0)
    : {
    : printInvalidMsg();
    : }
    :
    : while (side1 >0 && side2 >0 && side3 >0)
    : {
    : {
    : if (side1 == side2 && side2 == side3)
    : eq_area = areaEquilateral(side1, side2, side3);
    : printEquilateralMsg(outfile);
    : [b]else[/b]
    : area = areaTriangle(side1, side2, side3);
    :
    : }
    : p = perimeterTriangle(side1, side2, side3);
    : printResults(outfile);
    : readData(infile, side1, side2, side3);
    : }
    :
    : infile.close();
    : outfile.close();
    :
    : system("PAUSE>NUL");
    : return 0;
    : }
    :
    : double perimeterTriangle(double side1, double side2, double side3)
    : {
    : double p;
    :
    : p = (side1 + side2 + side3);
    :
    : return p;
    : }
    :
    : double areaEquilateral(double side1, double side2, double side3)
    : {
    : double eq_area;
    :
    : eq_area = (side1 * side1 * sqrt (3) ) / 4;
    :
    : return eq_area;
    : }
    :
    : double areaTriangle(double side1, double side2, double side3)
    : {
    : double area,
    : p;
    :
    : p = perimeterTriangle (side1, side2, side3) / 2.0;
    : area = sqrt ( p * ( p - side1 ) * ( p - side2 ) * ( p - side3 )
    : );
    :
    : return area;
    : }
    :
    : void printIdInfo(ostream & out)
    : {
    : out <<"Jennifer Harrow
    "
    : <<"C.S.1428.3
    "
    : <<"11/15/07

    ";
    : }
    :
    : void readData(ifstream & infile, double &side1, double &side2,
    : double &side3)
    : {
    : infile >> side1 >> side2 >> side3;
    : }
    :
    : void printInvalidMsg()
    : {
    : cout = { [ ; ]* } | ;
    and if requires a block:
    = if() [ elseif() ]* [ else ]

    Which in case you don't know BNF means that if statements require blocks, and a block is either a single statement or a pair of curly parenthesises with zero or more statements in it.
  • jennbojennbo Posts: 3Member
    : Please use code tags when posting code.
    :
    : Also you should have pointed out the function where it occurred and
    : the whole error message.
    :
    : [code]:
    : : #include
    : : #include
    : : #include
    : : #include
    : :
    : : using namespace std;
    : :
    : : double areaEquilateral(double, double, double);
    : : double perimeterTriangle(double, double, double);
    : : double areaTriangle(double, double, double);
    : : void printIdInfo(ostream &);
    : : void readData(ifstream &, double &, double &, double &);
    : : void printInvalidMsg();
    : : void printEquilateralMsg(ofstream &);
    : : void printResults(ofstream &);
    : :
    : : int main()
    : : {
    : : double side1,
    : : side2,
    : : side3,
    : : eq_area,
    : : area,
    : : p;
    : :
    : : ifstream infile;
    : : ofstream outfile;
    : :
    : : infile.open("C:\Users\Jennbo\Desktop\prog10_inp.txt");
    : :
    : : if (!infile)
    : : {
    : : cout << "Failed to open input file.
    ";
    : :
    : : system("PAUSE");
    : : return 2;
    : : }
    : :
    : : outfile.open("C:\Users\Jennbo\Desktop\prog10_out.txt");
    : :
    : : if (!outfile)
    : : {
    : : cout << "Failed to open output file.";
    : :
    : : system("PAUSE");
    : : return 2;
    : : }
    : :
    : : printIdInfo(cout);
    : : printIdInfo(outfile);
    : :
    : : readData(infile, side1, side2, side3);
    : :
    : : if (side1 <= 0 || side2 <= 0 || side3 <= 0)
    : : {
    : : printInvalidMsg();
    : : }
    : :
    : : while (side1 >0 && side2 >0 && side3 >0)
    : : {
    : : {
    : : if (side1 == side2 && side2 == side3)
    : : eq_area = areaEquilateral(side1, side2, side3);
    : : printEquilateralMsg(outfile);
    : : [b]else[/b]
    : : area = areaTriangle(side1, side2, side3);
    : :
    : : }
    : : p = perimeterTriangle(side1, side2, side3);
    : : printResults(outfile);
    : : readData(infile, side1, side2, side3);
    : : }
    : :
    : : infile.close();
    : : outfile.close();
    : :
    : : system("PAUSE>NUL");
    : : return 0;
    : : }
    : :
    : : double perimeterTriangle(double side1, double side2, double side3)
    : : {
    : : double p;
    : :
    : : p = (side1 + side2 + side3);
    : :
    : : return p;
    : : }
    : :
    : : double areaEquilateral(double side1, double side2, double side3)
    : : {
    : : double eq_area;
    : :
    : : eq_area = (side1 * side1 * sqrt (3) ) / 4;
    : :
    : : return eq_area;
    : : }
    : :
    : : double areaTriangle(double side1, double side2, double side3)
    : : {
    : : double area,
    : : p;
    : :
    : : p = perimeterTriangle (side1, side2, side3) / 2.0;
    : : area = sqrt ( p * ( p - side1 ) * ( p - side2 ) * ( p - side3 )
    : : );
    : :
    : : return area;
    : : }
    : :
    : : void printIdInfo(ostream & out)
    : : {
    : : out <<"Jennifer Harrow
    "
    : : <<"C.S.1428.3
    "
    : : <<"11/15/07

    ";
    : : }
    : :
    : : void readData(ifstream & infile, double &side1, double &side2,
    : : double &side3)
    : : {
    : : infile >> side1 >> side2 >> side3;
    : : }
    : :
    : : void printInvalidMsg()
    : : {
    : : cout << "There were no valid input values on the input file.";
    : : }
    : :
    : : void printEquilateralMsg(ofstream & outfile)
    : : {
    : : outfile << "The triangle is an equilateral triangle.
    ";
    : : }
    : :
    : : void printResults(ofstream & outfile)
    : : {
    : : double side1,
    : : side2,
    : : side3,
    : : eq_area,
    : : p;
    : :
    : : outfile << "The sides of the triangle are " << setprecision(1)
    : : << fixed << side1 << ", " << side2 << ", and " << side3
    : : << ".
    "
    : : << "The area of the triangle is " << eq_area << ".
    "
    : : << "The perimeter of the triangle is " << p << ".

    ";
    : : }
    : :
    : [/code]:
    :
    : Your problem is that you don't know that (or can't apply that
    : knowledge) (in BNF, written from head):
    : <block> = { [ ; ]* } | ;
    : and if requires a block:
    : = if() [ elseif() ]*
    : [ else ]
    :
    : Which in case you don't know BNF means that if statements require
    : blocks, and a block is either a single statement or a pair of curly
    : parenthesises with zero or more statements in it.

    I apologize for not posting the code properly & for not including the entire error message, my mistake.

    Thank you very much for your response, it helped me to finally see what I was doing wrong. Got my curly parentheses in the correct order and lo and behold I was able to compile and make other needed changes. Thanks again!




Sign In or Register to comment.