Click to See Complete Forum and Search --> : Error is driving me crazy


Villainous1
April 29th, 2008, 12:59 AM
I am doing a very basic program that takes the length of the sides of a triangle or rectangle, calculates the perimeter then outputs it to a file.

I am getting the error :

fatal error C1075: end of file found before the left brace '{'



but i can't figure it out.
I will attach the cpp file.
Any help would be extremely helpful

Thanks!

7stud
April 29th, 2008, 01:19 AM
Post your code--do not attach files. I'm not in the habit of downloading programs that could be viruses from strangers who post on the internet.

sunil_cguru
April 29th, 2008, 01:20 AM
Thats bcoz your file formatting is all wrong.

Your classes 'triangle' and 'rectangle' are not closed
In C++ all the class declarations should be closed with a };
Also your main should be global here it seems to be within storedata().
Have a look at a proper C++ sample and you would figure out your mistakes

Villainous1
April 29th, 2008, 01:51 AM
Thanks for the input. I put a closing brace for the rectangle and triangle class.

I am getting an error with my second class it says:
error C2236: unexpected 'class' 'rectangle'. Did you forget a ';'?


But I structured it identically to the first class. Can anyone see what I did incorrectly?
THANKS!



here is the code:

#include <iostream>
#include <fstream> //for file interaction

using namespace std;


class triangle
{
public:

int sideA;
int sideB;
int sideC;
int perimeter;


public:
//constructor
triangle()
{
sideA = 0;
sideB = 0;
sideC = 0;
perimeter = 0;


}


void getdata()
{

cout << "Enter the length of Side A ";
cin >> sideA;
cout << "Enter the length of Side B";
cin >> sideB;
cout << "Enter the length of Side C";
cin>> sideC;

}
void calc()
{
perimeter = sideA + sideB + sideC;

}
void Tri_type()
{
if(sideA+sideB<sideC||sideB+sideC<sideA||sideA+sideC<sideB)
cout<<sideA<<','<<sideB<<','<<sideC<<" is"<<" NOT A TRIANGLE"<<endl;
else if(sideA==sideB&&sideB==sideC)
EQUILATERAL=type;
else if(sideA==sideB||sideB==sideC||sideA==sideC)
ISOSCELES=type;
else
SCALENE=type;

}

void printdata()
{
cout << "Perimeter of the Triangle is" << perimeter;
cout << endl;
cout << "Type of Triangle is" << type;
cout << endl;



}
void storedata()
//end of while
{
char another = ' ';
triangle triangleinfo;
fstream fileObj;

//opens files to prepare for writing out and appending
fileObj.open("triangleinfo.txt", ios::out | ios::app);

do
{
trianlgeinfo.getdata();

fileObj << triangleinfo.sideA << " " << triangleinfo.sideB << " " << triangleinfo.sideC << " "<< triangleinfo.perimeter<< " " <<triangleinfo.type<<endl;

cout << "Another " ;
cin >> another;
} while (another == 'y');

fileObj.close(); //closes file
system("pause");
}


}

class rectangle
{
public:

int sideA;
int sideB;
int perimeter;



public:
//constructor
rectangle()
{

sideA = 0;
sideB = 0;

perimeter = 0;

}


void getdata()
{

cout << "Enter the length of Side A ";
cin >> sideA;
cout << "Enter the length of Side B";
cin >> sideB;


}
void calc()
{
perimeter = sideA * sideB;

}


void printdata()
{
cout << "Perimeter of the Rectanlge is" << perimeter;
cout << endl;
cout << endl;



}
void storedata()
//end of while
{
char another = ' ';
rectangle rectangleinfo;
fstream fileObj;

//opens files to prepare for writing out and appending
fileObj.open("rectangleinfo.txt", ios::out | ios::app);

do
{
rectangleinfo.getdata();

fileObj << rectangleinfo.sideA << " " << rectangleinfo.sideB << " " << rectangleinfo.perimeter<<endl;

cout << "Another " ;
cin >> another;
} while (another == 'y');

fileObj.close(); //closes file
}
}

int main()
{

int option = 0; //resets to 0

while((option <=0) || (option >=2))
{

cout << "Enter 1 for TRIANGLE" << endl;
cout << "Enter 2 for RECTANGLE" << endl;
cout << "Enter now -> ";
cin >> option;

if ((option <=0) || (option >=2))
{
cout << "BAD INPUT-- try again!!!" <<endl;
system("pause");
system("cls");


}//end of if

}


switch(option)
{
case 1:
{
triangle mytriangle;
mytriangle.getdata();
mytriangle.calc();
mytriangle.Tri_type();
mytriangle.storedata();
mytriangle.printdata();

break;
}

case 2:
{
rectangle myrectangle;
myrectangle.getdata();
myrectangle.calc();
myrectangle.storedata();
myrectangle.printdata();

break;
}




return 0;
}
}

sunil_cguru
April 29th, 2008, 02:15 AM
Still can see lotz of issues in your code.
1) Move all your methods from SetData() to storedata() inside the class
2) Close the class with };
3) Keep your main as global i.e outside all your classes.

I suggest you write some simple C++ classes and understand the syntax before proceeding.

S_M_A
April 29th, 2008, 02:16 AM
Placing of main is ok. You have some flaws regarding class declarations and usage though. That or you're trying to make everything inline. If so, the closing brace should be after all methods have been defined. I.e. you should only have the declaration part below but with the addition of full implementations of the methods.

Declaration of a classclass Name {
int member1;
int member2;
public:
Name();
void method1();
void method2();
};
Implementation of the class:Name::Name()
{
// Whatever
}
Name::Method1()
{
// Whatever
}
Name::Method2()
{
// Whatever
}
Using the class:int main()
{
Name stackInstance;
Name* heapInstance = new Name;
stackInstance.Method1();
heapInstance->Method2();
delete heapInstance;
return 0;
}

Graham
April 29th, 2008, 03:56 AM
The compiler told you exactly what the problem was:

error C2236: unexpected 'class' 'rectangle'. Did you forget a ';'?

Look at this portion of your code:

} <<<< You forgot a ';'

class rectangle
{

Do please carefully read the messages that the compiler gives you.

Of course, there are other issues - as others have pointed out - but the solution to this one was handed to you.

Villainous1
April 29th, 2008, 04:29 AM
I have rewritten the program from scratch, figuring my other one was too far gone.

I have it working fine, but I have one problem perhaps you could help with.

After I selected triangle or rectangle and input the variables, it outputs the data then asks me to unput the variables again before it ends.

I want to get rid of that 2nd round of inputting


Any suggestions:


#include <iostream>
#include <fstream>
using namespace std;


//parent class
class shape
{
private:



protected:
shape()
{

}

void getdata()
{


}
void printdata()
{
}

}; //end of car

class triangle: public shape //child class
{
private:
int sideA;
int sideB;
int sideC;
char type[15];
int perimeter;


public:
triangle()
{
sideA = 0;
sideB = 0;
sideC = 0;
strcpy_s(type, " ");
perimeter = 0;

}
void getdata()
{
shape::getdata(); //calls parent class function
cout << "Enter the length of side A: ";
cin >> sideA;
cout << "Enter the length of side B: ";
cin >> sideB;
cout << "Enter the length of side C: ";
cin >> sideC;

}
void calc()
{
perimeter = sideA + sideB + sideC;

if(sideA==sideB&&sideB==sideC)
strcpy_s(type, "Equilateral");
else if(sideA==sideB||sideB==sideC||sideA==sideC)
strcpy_s(type, "Isosceles");
else
strcpy_s(type, "Scalene");

}

void printdata()
{
shape::printdata();
cout << "The Perimeter is: " << perimeter << endl;
cout << "The type of triangle is: " << type << endl;


{
char another = ' ';
triangle triangleinfo;
fstream fileObj;

//opens files to prepare for writing out and appending
fileObj.open("triangleinfo.txt", ios::out | ios::app);

do
{
triangleinfo.getdata();
triangleinfo.calc();

fileObj << triangleinfo.type<< " " << triangleinfo.sideA << " " << triangleinfo.sideB << " " << triangleinfo.sideC << " "<< triangleinfo.perimeter<< " " <<triangleinfo.type<<endl;





} while (another == 'y');
fileObj.close(); //closes file

}


}

}; //end of book

class rectangle: public shape //child class
{
private:
int sideA;
int sideB;
int perimeter;



public:
rectangle()
{
sideA = 0;
sideB = 0;
perimeter = 0;

}
void getdata()
{
shape::getdata(); //calls parent class function
cout << "Enter the length of side A: ";
cin >> sideA;
cout << "Enter the length of side B: ";
cin >> sideB;

}
void calc()
{
perimeter = sideA * sideB;

}

void printdata()
{
shape::printdata();
cout << "The Perimeter of your rectangle is: " << perimeter << endl;

{
char another = ' ';
rectangle rectangleinfo;
fstream fileObj;

//opens files to prepare for writing out and appending
fileObj.open("rectangleinfo.txt", ios::out | ios::app);

do
{
rectangleinfo.getdata();

fileObj << rectangleinfo.sideA << " " << rectangleinfo.sideB << " " << rectangleinfo.perimeter<<endl;

cout << "Another " ;
cin >> another;
} while (another == 'y');

fileObj.close(); //closes file



}
}

}; //end of tape


int main()
{

int option = 0; //resets to 0

while((option <=0) || (option >=3))
{

cout << "Enter 1 for TRIANGLE: " << endl;
cout << "Enter 2 for RECTANGLE: " << endl;
cout << "Enter now -> ";
cin >> option;

if ((option <=0) || (option >=3))
{
cout << "BAD INPUT-- try again!!!" <<endl;
system("pause");
system("cls");

}//end of if

} //end of while
//-----------------END OF RANGE CHECKINg-----------------

switch(option)
{
case 1:
{triangle mytriangle;
mytriangle.getdata();
mytriangle.calc();
mytriangle.printdata();
break;
}

case 2:
{
rectangle myrectangle;
myrectangle.getdata();
myrectangle.calc();
myrectangle.printdata();

break;
}



return 0;
}






}

sunil_cguru
April 29th, 2008, 04:52 AM
thats because inside triangle printdata() you call getdata() again after outputting the values

Villainous1
April 29th, 2008, 05:02 AM
I see, but I want the option to run the program again, without calling getdata() again, the option for "another" doesn't work.


Any suggestions?