error message help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teddarr
    New Member
    • Oct 2006
    • 143

    #1

    error message help

    I have an assignment I've been working on for a few days now. I need some help with the last detail.

    The program is supposed to create 5 objects with varying floating-point parameter lists. The class (Rectangle) is supposed to calculate the perimeter and area and tell whether the shape is a square or not. Then put this info in a table and sent to an external file. The limits on the sides of the rectangle is greater than 0.0 and no more than 20.0. I created 3 overloaded constructors. I reassigned values to 2 of the objects. then create a destructor that sends a message that tells which object is being destroyed. There are also get and set functions for length and width.

    The problem is that 1 of the objects is out of limits by design (length and width are greater than 20.0) I have error messages to acknowledge this but I want the messages to identify which object was out of limits. I have tries an if statement in the set function. I've tried passing another parameter to the set function, I've tried everything I can think of. Some of my commented out statements are still in the source code.

    Below is the source code (header, .cpp, and main). It works with no errors as is. Please help me with my error messages.


    Rectangle.h:
    Code:
     #ifndef RECTANGLE_H
    #include <iostream>
    
    using namespace std;
    
    class Rectangle
    {
    public:
    	Rectangle();
    	Rectangle(float len);
    	Rectangle(float len, float wid);
    	static void heading();
    	void doIt(int);
    	float perimeter();
    	float area();
    	void setLength(float);
    	float getLength();
    	void setWidth(float);
    	float getWidth();
    	char square();
    	~Rectangle();
    		
    private:
    	float length;
    	float width;
    	int objID;
    	void errorLen();
    //	void errorLen(int);
    	void errorWid();
    	
    };
    
    #endif
    Rectangle.cpp (class Rectangle)
    Code:
     #include <fstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    #include "Rectangle.h"
    
    extern ofstream Fileout;
    
    //constructor with no parameters
    Rectangle::Rectangle()
    {
    	setLength(1.0);	//default value
    	setWidth(1.0);	//default value
    }
    //constructor with 1 parameter
    Rectangle::Rectangle(float len)
    {
    	setLength(len);
    	setWidth(1.0);	//default value
    }
    
    //constructor with 2 parameters
    Rectangle::Rectangle(float len, float wid)
    {
    	setLength(len);	
    	setWidth(wid);
    }
    
    //setLength(), verify 0.0 < value <= 20.0, if not send error message
    void Rectangle::setLength(float len)
    {	
    //	int i = 1;
    	if (len > 0.0 && len <= 20.0)
    	{
    		length = len;
    //		i++;
    	}
    	else
    	{
    		length = 1.0;
    		errorLen();
    //		errorLen(i);	//find a way to set i to the object number
    //		i++;			//for setting error message to identify 
    	}					//object 4 as the object that is out of scope
    }
    
    //getLength()******************************
    float Rectangle::getLength()
    {
    	return length;
    }
    
    //setWidth(), verify 0.0 < value <= 20.0, if not send error message
    void Rectangle::setWidth(float wid)
    {
    	if (wid > 0.0 && wid <= 20.0)
    		width = wid;
    	else
    	{
    		width = 1.0;
    		errorWid();
    	}
    }
    
    //getWidth()****************************
    float Rectangle::getWidth()
    {
    	return width;
    }
    
    //heading()******************************
    void Rectangle::heading()
    {
    	Fileout<<setw(20)<<"Length"<<setw(10)<<"Width"<<setw(12)<<"Perimeter"<<setw(7)
    		<<"Area"<<setw(10)<<"Square"<<endl;
    	Fileout<<setw(60)<<"-----------------------------------------------"<<endl;
    }
    
    //doIt()**********************************
    void Rectangle::doIt(int objNum)
    {	
    	objID = objNum;	
    //	if (len <= 0.0 && len > 20.0)	//these are attempts at getting a number assigned
    //		errorLen(objID);			//the out of scope error message for object 4.
    	Fileout<<"Object "<<objNum<<setw(12)<<getLength()<<setw(10)<<getWidth()<<setw(10)
    		<<perimeter()<<setw(9)<<area()<<setw(8)<<square()<<endl;
    	
    }
    //float perimeter()********************************
    float Rectangle::perimeter()
    {
    	float p = 0;
    	p = (length*2) + (width*2);
    	return p;
    }
    //float area()**************************************
    float Rectangle::area()
    {
    	float a = 0;
    	a = length * width;
    	return a;
    }
    
    //bool square()***************************************
    char Rectangle::square()
    {
    	char sq;
    	if (length <= (width+0.0001) && length >= (width-0.0001))
    		sq = 'Y';
    	else
    		sq = 'N';
    	return sq;	
    }
    
    //errorLen()*****************************************
    //void Rectangle::errorLen(int objID)
    void Rectangle::errorLen()
    {	
    	cout<<"*ERROR* Object "<<"objID"<<" was created with invalid length,\n";
    	cout<<"this value has been changed to 1.0"<<endl<<endl;
    	//separate errormsg for length and width and pass parameter designating which object
    }
    
    //errorWid()*****************************************
    void Rectangle::errorWid()
    {	
    	cout<<"*ERROR* Object "<<"objNum "<<"was created with invalid width,\n";
    	cout<<"this value has been changed to 1.0"<<endl<<endl;
    	//separate errormsg for length and width and pass parameter designating which object
    }
    
    //destructor, message: "gone out of scope"***********
    Rectangle::~Rectangle()
    {
    	cout<<"Object "<<objID<<" has gone out of scope"<<endl;
    }
    RectangleMain.c pp
    Code:
    #include "Rectangle.h"
    #include <iomanip>
    #include <fstream>
    
    ofstream Fileout;
    
    void main()
    {
    	Fileout.open("output.txt"); //opens the output file.
    
    	//declare the initial 5 objects with the appointed values
    	Rectangle r1;
    	Rectangle r2(7.1, 3.2);
    	Rectangle r3(6.3);
    	Rectangle r4(21.0, 22.0);
    	Rectangle r5 = r2;
    
    	//following commands hold the print instructions to external file
    	Rectangle::heading();
    	r1.doIt(1);   
    	r2.doIt(2);
    	r3.doIt(3);
    	r4.doIt(4);
    	r5.doIt(5);
    	Fileout<<endl;
    
    	//Rectangle r6(5.4, 10.5);	//declare another object with new values
    	r1.setLength(5.4);
    	r1.setWidth(10.5);//reset r1 to hold values of r6
    	
    	//following commands hold the print instructions
    	Rectangle::heading();
    	r1.doIt(1);
    	r2.doIt(2);
    	r3.doIt(3);
    	r4.doIt(4);
    	r5.doIt(5);
    	cout<<endl;
    }
  • enri
    New Member
    • Feb 2007
    • 10

    #2
    tedarr,

    have you tried using

    Code:
    this->objID
    to get the rectangle ID in the error-reporting functions?

    Comment

    • teddarr
      New Member
      • Oct 2006
      • 143

      #3
      Thanks,
      I'll give it a try and let you know how it worked.

      Comment

      • teddarr
        New Member
        • Oct 2006
        • 143

        #4
        I tried using "this->objID in the errorLen() function. I get a junk number. This is the same number I get when I use objNum as the variable. It's not working for me.

        How can you get this to work?

        Thanks

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          The class variable objID is set in the doIt method, and you're accessing objID in the constructor, before it is set. That's why you're getting the garbage values.

          If you want to use objID in errors in the constructor, you'll need to set it there.

          Comment

          • teddarr
            New Member
            • Oct 2006
            • 143

            #6
            OK, but tis is where I'm having trouble. I can't find a method for doing this. Please help.

            Thanks

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by teddarr
              OK, but tis is where I'm having trouble. I can't find a method for doing this. Please help.

              Thanks
              He's suggesting you create an object ID in the constructor, so the method that sets the initial values of the rectangle are also going to create an object ID for it. You will have to change your Rectangle definition files.

              Comment

              • teddarr
                New Member
                • Oct 2006
                • 143

                #8
                RRick and Sicarie,

                Much thanks, You guys gave me enough to get me going. Program works how I want it now.

                Thanks again!

                Comment

                Working...