Ok the problem im having is in my setRectangleLength and setRectangleWidth. The compiler shows no errors so everything is fine. However when I try to validate the length and width between the values of 1-20 it does not work. When the program asks the user to enter another length or width, it does not replace the out of range length or width. So if i were to enter 21 for length and width, a message would say enter a value in range. If I then enter lets say 10 and 5, it will not take that as the length and witdh. I still takes 21 and 21 as the length and width. Any suggestions?

#include <iostream>
using namespace std;

class Rectangle
{
public:
	Rectangle()//rectangle constructor defaults the values for length and width to 1
	{
		rectangleLength = rectangleWidth = 1;
		rPerimeter = rArea = 1;
	}

	void setRectangleLength(int x)// set length value between 1 to 20
	{
		bool lengthTest = false;
		
		while ( !lengthTest )
		{
			if ( x > 0 && x <= 20 )
			{			
				lengthTest = true;
				rectangleLength = x;
			}
			if ( x < 0 && x >= 20)
			{
				cout << "The value " << x << " is out of range.";
				cout << "Try again. Choose a length from 1 to 20:";
				cin >> x;
				lengthTest = false;
			}
		}
	}

	int getRectangleLength()
	{
		return rectangleLength;
	}

	void setRectangleWidth(int y)// set width value between 1 to 20
	{
		bool widthTest = false;
		
		while ( !widthTest )
		{
			if ( y > 0 && y <= 20 )
			{			
				widthTest = true;
				rectangleWidth = y;
			}
			else 
			{
				cout << "The value " << y << " is out of range.";
				cout << "Try again. Choose a width from 1 to 20:";
				cin >> y;
				widthTest = false;
			}
		}
	}
	
	int getRectangleWidth()
	{
		return rectangleWidth;
	}
	
	
	void calculatePerimeter(int x, int y)
	{
		rPerimeter = 2 * (x * y);
		cout << "The perimeter of the triangle is: " << rPerimeter << endl;
	
	}

	void calculateArea(int x, int y)
	{
		rArea = x * y;
		cout << "The area of the triangle is: " << rArea << endl;
	
	}

	void setDrawRectangle(char b, char f)
	{
		border = b;
		fill = f;
	}

	char getDrawRectangle()
	{
		return border;
	}
	
	
	void draw()
	{
		cout << "Enter the character you would like to see as the border of the rectangle:";
		cin >> border;
		cout << "Enter the symbol to fill the rectangle:";
		cin >> fill;
		setDrawRectangle(border, fill);
		for (int i = 0; i < rectangleLength; i++)
		{
			for (int j = 0; j < rectangleWidth; j++)
			{				
				if ( i == 0  || i == rectangleLength - 1)
				{
					cout << border;

				}else if ( j == 0  || j == rectangleWidth - 1)
				{
					cout << border;
				}
				else 
				{
					cout << fill;
				}
			}
			cout << "\n";
		}
			
	}
private: // length and width as private data members
	int rectangleLength;
	int rectangleWidth;
	int rPerimeter;
	int rArea;
	char border;
	char fill;
};

int main()
{
	int length;
	int width;
	Rectangle r; // instantiate object r of class Rectangle

	cout << "Enter the length of the triangle from 1 to 20:";
	cin >> length;
	r.setRectangleLength(length);

	cout << "Enter the width of the triangle from 1 to 20:";
	cin >> width;
	r.setRectangleWidth(width);

	r.calculatePerimeter(length, width);
	r.calculateArea(length, width);
	r.draw();

	return 0;
}

Recommended Answers

All 2 Replies

I can see 2 things. I'm only going to discuss the first, if you correct it properly, the second issue should disappear.

Your member function Rectangle::calculateArea() really shouldn't have any arguments. It should use the information already contained within the class to determine its result, not passed in values. Passing the values in completely eliminates the reason for having the class in the first place.

I can see 2 things. I'm only going to discuss the first, if you correct it properly, the second issue should disappear.

Your member function Rectangle::calculateArea() really shouldn't have any arguments. It should use the information already contained within the class to determine its result, not passed in values. Passing the values in completely eliminates the reason for having the class in the first place.

thx for the help i just looked at it again i realized the stupid mistake i made lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.