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:
Rectangle.cpp (class Rectangle)
RectangleMain.c pp
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
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;
}
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;
}
Comment