Inputing Values for Length and Width?

How can I allow the user to input values for length and width? When I debug the code, it doesn't allow me to input anything.
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  #include <iostream> 
using namespace std;

class Rectangle
{
public:
	// Can be accessed by any part of the program
	int x;
	int y;


	int SetValues(int, int);
	int GetValues()
	{
		return x, y;
	}
	int GetArea() {return width*length; }
	int GetPerimeter() { return width * 2 + length * 2; };

private: // Can only be accessed by data members within the function
	int Rectangle::rectdefault(int width , int length);
	int rectdefault()
	{
		int GetArea();
		int GetPerimeter();

		return 0;
	}
	int width = 1;
	int length = 1;
};

int Rectangle::SetValues(int x, int y)
{
	Rectangle rect1, rect2, rect3, rectdefault; // This is where the constructors are called and instantiated
	x = 1;
	y = 1;


	width = x;
	length = y;


	rect1.SetValues(width, length);
	{
		cout << "Enter the width;";
		cin >> x;
		cout << "Enter the length :";
		cin >> y;

		return 0;
	}


	rect2.SetValues(width, length);
	{
		cout << "Enter the width;";
		cin >> x;
		cout << "Enter the length :";
		cin >> y;

		return 0;
	}

	

	rect3.SetValues( width, length);
	{
		cout << "Enter the width;";
		cin >> x;
		cout << "Enter the length :";
		cin >> y;

		return 0;
	}

	rectdefault.SetValues(1,1);
	return 0;
}

int main()
{
	
	int x = 1;
	int y = 1;



	Rectangle rect1, rect2, rect3,rectdefault; // This is where the constructors are called and instantiated
	
	rect1.SetValues(x, y);
	rect2.SetValues(x, y);
	rect3.SetValues(x, y);
	rectdefault.SetValues(1, 1);

	cout << rect1.GetValues();
	cout << rect2.GetValues();
	cout << rect3.GetValues();


	cout << "rect1 area: " << rect1.GetArea() << endl; // Retrieves area from the member function
	cout << "rect2 area: " << rect2.GetArea() << endl;
	cout << "rect3 area: " << rect3.GetArea() << endl;
	cout << "rectdefault area: " << rectdefault.GetArea() << endl;

	cout << "rect1 perimeter: " << rect1.GetPerimeter() << endl; // Retrieves perimeter from member function from class
	cout << "rect2 perimeter: " << rect2.GetPerimeter() << endl;
	cout << "rect3 perimeter: " << rect3.GetPerimeter() << endl;
	cout << "rectdefault perimeter: " << rectdefault.GetPerimeter() << endl;

	return 0;
}
Line 8-9: What is the purpose of x and y? And why are they public?

Line 15: You can't return two values like that. GetValues() can only return a single int.

Line 21: Class name should not be specified here. You have no implementation of this function.

Line 24-25: These are function declarations and do not belong here.

Line 35: Why are you declaring 4 Rectangles within this function?

Line 36-37: You're modifying the arguments, not the class members.

Line 79: rect1, rect2, rect3 and rectdefault are all local variables that go out of scope when the function exists.

Lines 45-52,56-63,68-75: These braces are unnecessary. They makes lines 44,55,67 look like function definitions, which they are not.

Lines 47,49,58,60,70,72: You're modifying the instance's x any with, not length and width.

Lines 44,55,67: You're making recursive calls to SetValues().

Line 51: You're going to return unconditionally and never get to the following code.

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
{   int width;
    int length;
    
public:
	//  Got rid of x and x 

    //  Default constructor
    Rectangle ()
    {   width = 1;
        length = 1;
    }
    //  Parameterized constructor
    Rectangle (int w, int l)
    {   width = w;
        length = l;
    }        
	void PromptValues ()
    {   cout << "Enter the width;";
	    cin >> width;
        cout << "Enter the length :";
	    cin >> length;
    }	    	
	int GetWidth () const
	{   return width;
	}
	int GetLength() const
    {   return length; 
	}
	int GetArea() 
	{   return width*length; 
	}
	int GetPerimeter() 
	{   return width * 2 + length * 2; 
	}
};

int main()
{   Rectangle rect1;                //  Default constructor (1,1)
    Rectangle rect2 (1,1);          //  Explicit constructor
    Rectangle rect3;

	rect3.PromptValues();

	cout << rect1.GetWidth() << "," << rect1.GetLength() << endl;
	cout << rect2.GetWidth() << "," << rect2.GetLength() << endl;
	cout << rect3.GetWidth() << "," << rect3.GetLength() << endl;
	
	cout << "rect1 area: " << rect1.GetArea() << endl; // Retrieves area from the member function
	cout << "rect2 area: " << rect2.GetArea() << endl;
	cout << "rect3 area: " << rect3.GetArea() << endl;

	cout << "rect1 perimeter: " << rect1.GetPerimeter() << endl; // Retrieves perimeter from member function from class
	cout << "rect2 perimeter: " << rect2.GetPerimeter() << endl;
	cout << "rect3 perimeter: " << rect3.GetPerimeter() << endl;

    system ("pause");
	return 0;
}





Topic archived. No new replies allowed.