Rectangle program

Cannot figure out my mistake. Can someone please point it out? My friend also helped but can't figure out as well. Thanks!

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
#include <iostream>
using namespace std;

class Rectangle {
private:
	float length;
	float width;

	Rectangle()
	{
		width = 1.0;
		length = 1.0;
	}

	Rectangle(float l, float w) {
		float setLength(l);
		float setWidth(w);
	}

	float setLength(float l) 
	{
		if (l < 0 && l > 20) 
		{
			length = l;
		}
		else {
			cout << "Invalid value, please enter value between 0 and 20.";
		}
	}

	float setWidth(float w) 
	{
		if (w < 0 && w > 20) 
		{
			width = w;
		}
		else {
			cout << "Invalid value, please enter value between 0 and 20.";
		}
	}
	//class Rectangle() 
	//{
	//private:
		//float lenght, width;
	

	float getLength() {
		return length;
	}

	float getWidth() {
		return width;
	}

	float getArea() {
		return length * width;
	}

	float getPerimeter() {
		return 2 * (length + width);
	}
};

int main() {
	Rectangle::rect1 (length, width);
	//float length, width;

	cout << "Please enter the length value: ";
	cin >> length;

	cout << "Please enter the width value: ";
	cin >> width;

	//Rectangle (length, width);

	cout << "The rectangle area is: " << rect1.getArea() << endl;
	cout << "The perimeter is: " << rect1.getPerimeter() << endl;

	system("pause");
	return 0;
	//system("pause");
}
Last edited on
If ( w < 0 && w > 20)

How can a value be less than 0 AND greater than 20? It's going to evalute to false everytime
So I fixed it, but I still have a problem with my cpp file.

this is my h file
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
#include <iostream>
using namespace std;

//class Rectangle {
//private:
	//float length;
	//float width;
class Rectangle {
public:
	Rectangle()
	{
		width = 1.0;
		length = 1.0;
	}

	Rectangle(float l, float w) {
		float setLength(l);
		float setWidth(w);
	}

	float setLength(float l) 
	{
		if (l > 0 && l < 20) 
		{
			length = l;
		}
		else {
			cout << "Invalid value, please enter value between 0 and 20.";
		}
	}

	float setWidth(float w) 
	{
		if (w > 0 && w < 20) 
		{
			width = w;
		}
		else {
			cout << "Invalid value, please enter value between 0 and 20.";
		}
	}
	
	float getLength() {
		return length;
	}

	float getWidth() {
		return width;
	}

	float getArea() {
		return length * width;
	}

	float getPerimeter() {
		return 2 * (length + width);
	}
private:
	float length;
	float width;
	//float rect1;
};


when I run my cpp and enter a value, it's calculating the value of length and width in h file.
this is my cpp
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
#include <iostream>
#include "Rectangle.h"
using namespace std;

int main() {

		Rectangle rect1;
		float length, width;

		cout << "Please enter the length value: ";
		cin >> length;

		cout << "Please enter the width value: ";
		cin >> width;

		//Rectangle (length, width);

		cout << "The rectangle area is: " << rect1.getArea() << endl;
		cout << "The perimeter is: " << rect1.getPerimeter() << endl;

		system("pause");
		return 0;
		//system("pause");
	
}


@dullerthandull

I find it works this way..
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
#include <iostream>

using namespace std;

class Rectangle {
public:
	float length;
	float width;
	Rectangle()
	{
		width = 1.0;
		length = 1.0;
	}

	Rectangle(float l, float w) {
		float setLength(l);
		float setWidth(w);
	}

	float setLength(float l) // Doesn't seem to do anything
	{
		if (l > 0 && l < 20) 
		{
			length = l;
		}
		else {
			cout << "Invalid value, please enter value between 0 and 20.";
		}
	}

	float setWidth(float w)  // Doesn't seem to do anything
	{
		if (w > 0 && w < 20) 
		{
			width = w;
		}
		else {
			cout << "Invalid value, please enter value between 0 and 20.";
		}
	}

	float getLength() {
		return length;
	}

	float getWidth() {
		return width;
	}

	float getArea() {
		return length * width;
	}

	float getPerimeter() {
		return 2 * (length + width);
	}
};

int main()
{

	Rectangle rect1;
	
	cout << "Please enter the length value: ";
	cin >> rect1.length;

	cout << "Please enter the width value: ";
	cin >> rect1.width;

	// Rectangle (length, width);

	cout << "The rectangle area is: " << rect1.getArea() << endl;
	cout << "The perimeter is: " << rect1.getPerimeter() << endl;

	system("pause");
	return 0;
	//system("pause");
}
Last edited on
@whitenite1 thank you!
Topic archived. No new replies allowed.