HELLLPPPPPP loop problem

I successfully get the program to loop but unfortunately I cannot input data for my second formula and help would be welcome please and thank you!

##############################################################



#include <iostream>
using namespace std;

int main()
{
int length, width, perimeter, area, y, n, number = 1;


{

cout << "Enter length of rectangle: ";
cin >> length;
cout << "Enter width of rectangle: ";
cin >> width;
area = length*width;
perimeter = 2*(length+width);
cout << "The area of the rectangle is " << area << endl;
cout << "The perimeter of the rectangle is " << perimeter << endl;
cout << "Continue? (y/n) ";
cin >> y, n;

cin.ignore();

}

while (number = 2)



{
int length, width, perimeter, area, y, n, number = 2;

cout << "Enter length of rectangle: " << endl;
cin >> length;
cout << "Enter width of rectangle: " << endl;
cin >> width;
area = length*width;
perimeter = 2*(length+width);
cout << "The perimeter of the rectangle is" << perimeter << endl;
cout << "The area of the rectangle is" << area << endl;
cout << "Continue? (y/n)" << endl;

break;

}
{
cin.ignore();
cin.get();



}

return 0;
}



############################################################################
I refuse to read through that source if its not in code tags but just while scrolling down I can already see a problem in your code.

while (number = 2)
should be
while (number == 2)
Last edited on
cin >> y, n;

This line makes no sense. (atleast to me)
while (number = 2) was a problem but your exit conditions for the loop weren't correct either. The break will be called regardless of any conditions and so you will never loop.

The while needs to have a true/false statement that has a possibility of changing. This statement needs to be driven by a cin. I've made some changes below.

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

int main()
{
	int length, width, perimeter, area;
	char Cont;

	cout << "Enter length of rectangle: ";
	cin >> length;
	cout << "Enter width of rectangle: ";
	cin >> width;
	
	area = length*width;
	perimeter = 2*(length+width);
	
	cout << "The area of the rectangle is " << area << endl;
	cout << "The perimeter of the rectangle is " << perimeter << endl;
	cout << "Continue? (y/n) ";
	cin >> Cont;

	cin.get()

	while (Cont == "y")
	{
		cout << "Enter length of rectangle: " << endl;
		cin >> length;
		cout << "Enter width of rectangle: " << endl;
		cin >> width;
		
		area = length*width;
		perimeter = 2*(length+width);
		
		cout << "The perimeter of the rectangle is" << perimeter << endl;
		cout << "The area of the rectangle is" << area << endl;
		cout << "Continue? (y/n)" << endl;
		cin >> Cont;
	}

	cin.get();
	return 0;
}

Also, you pretty much run the same code twice. Unless this was intentional, you don't need to do this. You could shorten the program by initializing your entry condition.

Also, in the original code you initialized your parameters twice. You should probably just do it once.

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

int main()
{
	int length, width, perimeter, area;
	char Cont = "y";

	while (Cont == "y")
	{
		cout << "Enter length of rectangle: " << endl;
		cin >> length;
		cout << "Enter width of rectangle: " << endl;
		cin >> width;
		
		area = length*width;
		perimeter = 2*(length+width);
		
		cout << "The perimeter of the rectangle is" << perimeter << endl;
		cout << "The area of the rectangle is" << area << endl;
		cout << "Continue? (y/n)" << endl;
		cin >> Cont;
	}

	cin.get();
	return 0;
}
Topic archived. No new replies allowed.