What's wrong with my loop?

What's wrong with my loop? When i enter 0 for length, i want it to exit the program.

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
  #include <iostream>

using namespace std;

double getLength();
double getWidth();
double getArea(double length, double width);
double getPerimeter(double length, double width);
void displayData(double length, double width, double perimeter, double area);

int main()
{
	int length;
	int width;
	int perimeter;
	int area;

	do
	{
		if ( length < 0)
		{
			exit;
		}
		else
		{
			length = getLength();
			width = getWidth();
			area = getArea(length, width);
			perimeter = getPerimeter(length, width);
			displayData(length, width, perimeter, area);
		}
	} 
	while(length != 0);
	
	return 0;
}

double getLength()
{
	double length;
	
	

		cout << endl;
		cout << "\nPlease enter the length (in units) of the rectangle: ";
		cin >> length;
	
	return length;
}

double getWidth()
{
	double width;
	
	cout << endl;
	cout << "\nPlease enter the width (in units) of the rectangle: ";
	cin >> width;
	cout << "\n---------------------------------------------------" << endl;

	return width;
}

double getArea(double length, double width)
{
	double area;
	area = length * width;

	return area;
}

double getPerimeter(double length, double width)
{
	double perimeter;
	perimeter = 2 * (length + width);

	return perimeter;
}

void displayData(double length, double width, double perimeter, double area)
{
	cout << "\nThe length of the rectangle is: " << length << " units" << "." << endl << endl;
	cout << "\nThe width of the rectangle is: " << width << " units" << "." << endl << endl;
	cout << "\nThe perimeter of the rectangle is: " << perimeter << " units" << "." << endl << endl;
	cout << "\nThe area of the rectangle is: " << area << " units squared" << "." << endl << endl;
	cout << "---------------------------------------------------" << endl;

	return;
}
Try

if ( length <= 0)
That does not allow me to enter. It terminates at the start!
maybe not the best fix but this should work.

Line 13

int length=1;
1
2
3
4
	int length;
	int width;
	int perimeter;
	int area;


Uninitialised variables are very bad.
Topic archived. No new replies allowed.