CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2008
    Posts
    6

    Error is driving me crazy

    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!
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2004
    Posts
    296

    Re: Error is driving me crazy

    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.

  3. #3
    Join Date
    Apr 2008
    Posts
    133

    Re: Error is driving me crazy

    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

  4. #4
    Join Date
    Apr 2008
    Posts
    6

    Re: Error is driving me crazy

    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:
    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;
    				}
    }
    Last edited by Villainous1; April 29th, 2008 at 02:16 AM.

  5. #5
    Join Date
    Apr 2008
    Posts
    133

    Re: Error is driving me crazy

    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.

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Error is driving me crazy

    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 class
    Code:
    class Name {
      int member1;
      int member2;
    public:
      Name();
      void method1();
      void method2();
    };
    Implementation of the class:
    Code:
    Name::Name()
    {
       // Whatever
    }
    Name::Method1()
    {
       // Whatever
    }
    Name::Method2()
    {
       // Whatever
    }
    Using the class:
    Code:
    int main()
    {
      Name   stackInstance;
      Name* heapInstance = new Name;
      stackInstance.Method1();
      heapInstance->Method2();
      delete heapInstance;
      return 0;
    }

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Error is driving me crazy

    The compiler told you exactly what the problem was:

    Quote Originally Posted by compiler
    error C2236: unexpected 'class' 'rectangle'. Did you forget a ';'?
    Look at this portion of your code:
    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Apr 2008
    Posts
    6

    Re: Error is driving me crazy

    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:

    Code:
    #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;
    	}
    
    
    
    
    
    
    }

  9. #9
    Join Date
    Apr 2008
    Posts
    133

    Re: Error is driving me crazy

    thats because inside triangle printdata() you call getdata() again after outputting the values

  10. #10
    Join Date
    Apr 2008
    Posts
    6

    Re: Error is driving me crazy

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured