Displaying Array of Rectangles

I'm working on a project where the objective is to be able to insert information about the dimensions of an array of rectangles, then output the area and perimeter as well as implement a draw function to draw the shapes in the command window. It's all working except that I'm having trouble making the array interact with the draw function. It outputs the number of rectangles entered correctly, but it displays the same (incorrect) shape every time.

Any guidance would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "Rectangle.h"

int menu();

int main()
{
	Rectangle myRectangle[20];
	int l;
	int w;
	int choice;
	int nrR = 0;
  
   choice = menu();
   
   while ((choice >= 1) && (choice <= 5))
   {
   if (choice == 1) //entering dimensions of rectangle
   {   
   cout << "Enter length of rectangle: "; //taking user inputs
   cin >> l;
   cout << "Enter width of rectangle: ";
   cin >> w;
   myRectangle[nrR].setWidth(w);
   myRectangle[nrR].setLength(l);
   nrR++;
   }

   if(choice == 2) //prints the area of the rectangle
   {
	   for (int i = 0; i < nrR; i++)
		   myRectangle[i].printArea();
   }
   
   if(choice == 3) //prints the perimeter of the rectangle
   {
	   for (int i = 0; i < nrR; i++)
		   myRectangle[i].printPerimeter();
   }
  
   if (choice == 4) //draw the rectangle
   {
	   for (int i = 0; i < nrR; i++)
	   {
		   Rectangle myRectangle(l, w);
		   myRectangle.draw(l, w);
	   }
   }
  
   if (choice == 5) //exit the program
   {
	   break;
   }
   choice = menu();
   
   
} //end while
} //end main

int menu() //menu function
{
    int choice1;
    
    cout << "1. Enter rectangle dimensions: " << endl;
    cout << "2. Print rectangle area: " << endl;
    cout << "3. Print rectangle perimeter: " << endl;
	cout << "4. Draw rectangle: " << endl;
	cout << "5. Exit the program: " << endl;
    
    cout << "Enter your choice: ";
    cin >> choice1;
    return choice1;
}
Here are my other files as well, just in case they need changes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//Rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
#include <string>

using namespace std;

class Rectangle
{
public:
	Rectangle();
	Rectangle(int len, int wid);
	int getLength();
	int getWidth();
	void setLength(int l);
	void setWidth(int w);
	int getArea();
	int getPerimeter();
	void draw(int length, int width);
	void printPerimeter();
	void printArea();

private:
	int length;
	int width;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//rectangle.cpp
#include "Rectangle.h"

Rectangle::Rectangle()
{
}

//set functions
Rectangle::Rectangle(int len, int wid)
{
	length = len;
	width = wid;
}

void Rectangle::setLength(int l)
{
	length = l;
}

void Rectangle::setWidth(int w)
{
	width = w;
}

int Rectangle::getLength()
{    
     return length;
}

int Rectangle::getWidth()
{
	return width;
}

int Rectangle::getArea()
{
	return (length*width);
}

int Rectangle::getPerimeter()
{
	return (2 * (length + width));
}

void Rectangle::draw(int length, int width)
{
	char border, fill;
	cout << "Please choose a character for the shape's border: ";
	cin >> border;
	cout << "Please choose a character for the inside of the shape: ";
	cin >> fill;

for (int i = 0; i <= length; i++)
{
	cout << border;
}

cout << "\n";

for (int i = 0; i <= width; i++)
{
	cout << border;
	for (int i = 0; i <= length - 2; i++)
		{
			cout << fill;
		}
		cout << border << " \n";
}

for (int i = 0; i <= length; i++)
	{
		cout << border; //border
	}
cout << endl;
}


void Rectangle::printPerimeter()
{
	cout << "Perimeter of the rectangle is: "
		<< getPerimeter() << " units." << endl;
}

void Rectangle::printArea()
{
	cout << "Area of the rectangle is: "
		<< getArea() << " square units." << endl;
}
Topic archived. No new replies allowed.