Lost in the Loop

First I want to thank all the support for the community, you are all amazing. My issue is that when I run this program, it does not accept the "area" INT, is starts at the top of the loop and moves all the way through, even if I choose 2 to change the While, it continues one if statement after another, I have read, and checked everything I can, I would love some hints on why it is ignoring the input and just running through. Thank You again.

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>
#include <iomanip>
#include <math.h>
#include <string>

using namespace std;

int main()
{
    std::cout << std::fixed;
    std::cout << std::setprecision(2);

    //variable declaration
    float area;//type of area to solve for
    float side;//for the square
    float radius;//for the circle
    float radiussquared;//squared value of the radius
    float width;//for the rectangle
    float length;//for the rectangle
    float base;//for the triangle
    float height;//for the triangle
    float trianglearea;//area of Triangle
    float squarearea;//area of square
    float circlearea;//area of circle
    float rectanglearea;//area of rectangle
    int decider;//for deciding whether to run another loop

    //getting variable for while loop

    decider = 1;

    while (decider < 2) {
    cout << "Please enter number for type of area to solve for (1=square, 2=circle, 3=rectangle, 4=triangle) ";
    cin >> area;


    if  (area != 1);
            cout << "Please input the size of one of the sides of the square ";
            cin >> side;
            squarearea = side * side;
            cout << "the area of the square is " << squarearea << "\n";
            cout << "Do you wish to figure for another area? (1 for Yes) ";
            cin >> decider;

   if (area != 2);

            cout << "Please input the radius of the circle ";
            cin >> radius;
            radiussquared = radius * radius;
            circlearea = radiussquared * M_PI;
            cout << "The area of the Circle is " << circlearea << "\n";
            cout << "Do you wish to figure for another area? (1 for Yes, 2 for No) ";
            cin >> decider;


    if (area != 3);

            cout << "Please input the width of the rectangle ";
            cin >> width;
            cout << "Please input the length of the rectangle ";
            cin >> length;
            rectanglearea = width * length;
            cout << "The area of the Rectangle is " << rectanglearea << "\n";
            cout << "Do you wish to figure for another area? (1 for Yes, 2 for No) ";
            cin >> decider;

    if (area != 4);

            cout << "Please input the height of the triangle ";
            cin >> height;
            cout << "Please input the base of the triangle ";
            cin >> base;
            trianglearea = (height * base) / 2;
            cout << "The area of the Triangle is " << trianglearea << "\n";
            cout << "Do you wish to figure for another area? (1 for Yes, 2 for No) ";
            cin >> decider;


    }
    return 0;
}
closed account (2UD8vCM9)
Two reasons

First:
 
if (area !=1)


This is checking if area is NOT equal to 1 rather than checking if area is equal to 1
it should be
 
if (area == 1)


Also, you had semicolons after your if statements for some reason. You shouldn't.

Next

You're missing your opening/closing brackets for your if statements {'s and }'s

It should look like this

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
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>

#define M_PI 3.14159
using namespace std;

int main()
{
	std::cout << std::fixed;
	std::cout << std::setprecision(2);

	//variable declaration
	float area;//type of area to solve for
	float side;//for the square
	float radius;//for the circle
	float radiussquared;//squared value of the radius
	float width;//for the rectangle
	float length;//for the rectangle
	float base;//for the triangle
	float height;//for the triangle
	float trianglearea;//area of Triangle
	float squarearea;//area of square
	float circlearea;//area of circle
	float rectanglearea;//area of rectangle
	int decider;//for deciding whether to run another loop

	//getting variable for while loop

	decider = 1;

	while (decider < 2) 
	{ //While Start
		cout << "Please enter number for type of area to solve for (1=square, 2=circle, 3=rectangle, 4=triangle) ";
		cin >> area;

		if (area == 1)
		{
			cout << "Please input the size of one of the sides of the square ";
			cin >> side;
			squarearea = side * side;
			cout << "the area of the square is " << squarearea << "\n";
			cout << "Do you wish to figure for another area? (1 for Yes) ";
			cin >> decider;
		}

		if (area == 2)
		{
			cout << "Please input the radius of the circle ";
			cin >> radius;
			radiussquared = radius * radius;
			circlearea = radiussquared * M_PI;
			cout << "The area of the Circle is " << circlearea << "\n";
			cout << "Do you wish to figure for another area? (1 for Yes, 2 for No) ";
			cin >> decider;
		}

		if (area == 3)
		{
			cout << "Please input the width of the rectangle ";
			cin >> width;
			cout << "Please input the length of the rectangle ";
			cin >> length;
			rectanglearea = width * length;
			cout << "The area of the Rectangle is " << rectanglearea << "\n";
			cout << "Do you wish to figure for another area? (1 for Yes, 2 for No) ";
			cin >> decider;
		}

		if (area == 4)
		{
			cout << "Please input the height of the triangle ";
			cin >> height;
			cout << "Please input the base of the triangle ";
			cin >> base;
			trianglearea = (height * base) / 2;
			cout << "The area of the Triangle is " << trianglearea << "\n";
			cout << "Do you wish to figure for another area? (1 for Yes, 2 for No) ";
			cin >> decider;
		}

	} //End While
	return 0;
}
closed account (2UD8vCM9)
Also keep in mind that you can shorten your code by 6 lines by moving the

1
2
cout << "Do you wish to figure for another area? (1 for Yes, 2 for No) ";
cin >> decider;


Outside of your if statements and having it after all the if's have been checked.

For example
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
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>

#define M_PI 3.14159
using namespace std;

int main()
{
	std::cout << std::fixed;
	std::cout << std::setprecision(2);

	//variable declaration
	float area;//type of area to solve for
	float side;//for the square
	float radius;//for the circle
	float radiussquared;//squared value of the radius
	float width;//for the rectangle
	float length;//for the rectangle
	float base;//for the triangle
	float height;//for the triangle
	float trianglearea;//area of Triangle
	float squarearea;//area of square
	float circlearea;//area of circle
	float rectanglearea;//area of rectangle
	int decider;//for deciding whether to run another loop

	//getting variable for while loop

	decider = 1;

	while (decider < 2) 
	{ //While Start
		cout << "Please enter number for type of area to solve for (1=square, 2=circle, 3=rectangle, 4=triangle) ";
		cin >> area;

		if (area == 1)
		{
			cout << "Please input the size of one of the sides of the square ";
			cin >> side;
			squarearea = side * side;
			cout << "the area of the square is " << squarearea << "\n";
		}

		if (area == 2)
		{
			cout << "Please input the radius of the circle ";
			cin >> radius;
			radiussquared = radius * radius;
			circlearea = radiussquared * M_PI;
			cout << "The area of the Circle is " << circlearea << "\n";
		}

		if (area == 3)
		{
			cout << "Please input the width of the rectangle ";
			cin >> width;
			cout << "Please input the length of the rectangle ";
			cin >> length;
			rectanglearea = width * length;
			cout << "The area of the Rectangle is " << rectanglearea << "\n";
		}

		if (area == 4)
		{
			cout << "Please input the height of the triangle ";
			cin >> height;
			cout << "Please input the base of the triangle ";
			cin >> base;
			trianglearea = (height * base) / 2;
			cout << "The area of the Triangle is " << trianglearea << "\n";
		}

		cout << "Do you wish to figure for another area? (1 for Yes, 2 for No) ";
		cin >> decider;

	} //End While
	return 0;
}
This is also a good opportunity for a switch statement:
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
		switch (area) {
		case 1:
			cout << "Please input the size of one of the sides of the square ";
			cin >> side;
			squarearea = side * side;
			cout << "the area of the square is " << squarearea << "\n";
			break;

		case 2:
			cout << "Please input the radius of the circle ";
			cin >> radius;
			radiussquared = radius * radius;
			circlearea = radiussquared * M_PI;
			cout << "The area of the Circle is " << circlearea << "\n";
			break;

		case 3:
			cout << "Please input the width of the rectangle ";
			cin >> width;
			cout << "Please input the length of the rectangle ";
			cin >> length;
			rectanglearea = width * length;
			cout << "The area of the Rectangle is " << rectanglearea << "\n";
			break;
		case 4:
			cout << "Please input the height of the triangle ";
			cin >> height;
			cout << "Please input the base of the triangle ";
			cin >> base;
			trianglearea = (height * base) / 2;
			cout << "The area of the Triangle is " << trianglearea << "\n";
			break;
		}
TY, I made the changes, I like that it saves 6 lines of code as well. And it does work for the determining if I want to run another query. TY.

However, when I run it, it asks for which of the areas I want to solve for, and no matter what I pick, it still runs through all 4 Starting at number 1

Would a Break Command stop after it ran one of them? and should the 'Float area' be instead an 'int area'?
I will try the Switch command
See if that fixes
closed account (2UD8vCM9)
Can you tell me your exact input to get it to go through all four of them?

Because when I tried it with the code I pasted it was only solving for the one I had entered to solve for.

I'm wondering why it is doing that for you if you used the code I had pasted.
pindrought and dhayden,

Thank you so much for your help it is working perfectly now... I really appreciate it.

Amazing... :)
Would a Break Command stop after it ran one of them?

Yes. That's why I put then there :). Without a break, the code will fall through from one case to the next.

Since a missing break is such a common bug, any time I indent to fall through to the next case, I put a // FALLTHROUGH comment at the end of the case.
Thank you, using the //comments makes a world of difference.
Topic archived. No new replies allowed.