Can't firgure out how to validate the input correctly.

I'm trying to write a code for the good ol' geometry calculator problem. I got most of it figured out (I think) but I cant get it to validate it correctly. Any idea?
Here's my code. I played around with the return, which kinda helps.
Btw it option 1 at the moment. Valid inputs should only be 1-1000 only, which anything else should end the program but it still gives an answer.
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
  #include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
	double area = 0, length = 0, width = 0;
	double base = 0, height = 0, radius = 0, pie = 3.14159;
	int choice = 0;

	cout << "Geometry Calculator. " << endl;
	cout << "1. Calculate The Area of a Circle ." << endl;
	cout << "2. Calculate The Area of a Rectangle ." << endl;
	cout << "3. Calculate The Area of a Triangle. " << endl;
	cout << "4. Quit. " << endl;
	cout << "Please enter a choice (1-4) : " << endl;
	cin >> choice;

	if (choice >= 1 && choice <= 4)
	{

		switch (choice)
		{
		case 1:

			cout << "What is the radius of the circle?. ";
			cin >> radius;

			{
				if (radius >= 1 && radius <= 1000);
				else cout << "You've entered an invalid selection." ;
			int main;
			}

					area = pie * pow(radius, 2);

			cout << "The area of the Circle is " << area << endl;
			break;
		}
		switch (choice)

		{
		case 2:

			cout << "Please enter the width. ";
			cin >> width;

			cout << "Please enter the height. ";
			cin >> height;

			area = width * height;

			cout << "The area of the Rectangle is " << area << endl;
			break;
		}

		switch (choice)
		{
		case 3:

			cout << "Please enter the base. ";
			cin >> base;

			cout << "Please enter the height. ";

			cin >> height;

			area = .5 * base * height;

			cout << "This is the area of the Triangle. " << area << endl;
			break;
		}

	}

	return 0;

}
Do you get a compilation error? What's the problem?
For now try to remove line 33.
You only need ONE switch keyword, then list your cases inside this.

Also your brace placement is pretty messed up.

You want to do (maybe) something 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
switch (choice)
		{
		case 1:
			{
				cout << "What is the radius of the circle?. ";
				cin >> radius;

				if (radius >= 1 && radius <= 1000)
				{
					
					area = pie * pow(radius, 2);

					cout << "The area of the Circle is " << area << endl;
				}
				else
				{
					cout << "You've entered an invalid selection.";					
				}
				break;
			}
		case 2:
			{
				cout << "Please enter the width. ";
				cin >> width;

				cout << "Please enter the height. ";
				cin >> height;

				area = width * height;

				cout << "The area of the Rectangle is " << area << endl;
				break;
			}

		
		case 3:
			{
				cout << "Please enter the base. ";
				cin >> base;

				cout << "Please enter the height. ";

				cin >> height;

				area = .5 * base * height;

				cout << "This is the area of the Triangle. " << area << endl;
				break;
			}
		}
Hey fellas! Thanks for the help, but I was able to figure it out. Might not look your guys but it worked. Let me know you think!!!
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
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
	double area = 0, length = 0, width = 0;
	double base = 0, height = 0, radius = 0, pie = 3.14159;
	int choice = 0;

	cout << "Geometry Calculator. " << endl;
	cout << "1. Calculate The Area of a Circle ." << endl;
	cout << "2. Calculate The Area of a Rectangle ." << endl;
	cout << "3. Calculate The Area of a Triangle. " << endl;
	cout << "4. Quit. " << endl;
	cout << "Please enter a choice (1-4) : " << endl;
	cin >> choice;

	if (choice >= 1 && choice <= 4)
	{

		switch (choice)
		{
		case 1:

			cout << "What is the radius of the circle?. ";
			cin >> radius;

			{
				if (radius >= 1 && radius <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;
			}

			area = pie * pow(radius, 2);

			cout << "The area of the Circle is " << area << endl;
			break;
		}
		switch (choice)

		{
		case 2:

			cout << "Please enter the width. ";
			cin >> width;

			{
				if (width >= 1 && width <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;
			}

			cout << "Please enter the height. ";
			cin >> height;

			{
				if (height >= 1 && height <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;
			}

			area = width * height;

			cout << "The area of the Rectangle is " << area << endl;
			break;
		}

		switch (choice)
		{
		case 3:

			cout << "Please enter the base. ";
			cin >> base;

			{
				if (base >= 1 && base <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;
			}

			cout << "Please enter the height. ";

			cin >> height;
			{
				if (height >= 1 && height <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;
			}

			area = .5 * base * height;

			cout << "This is the area of the Triangle. " << area << endl;
			break;
		}

	}

	return 0;

}
And here's the finished product. Just need to add my comments and what not's. Any tips are greatly appreciated!!!
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
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
	double area = 0, length = 0, width = 0;
	double base = 0, height = 0, radius = 0, pie = 3.14159;
	int choice = 0;

	cout << "Geometry Calculator. " << endl;
	cout << "1. Calculate The Area of a Circle ." << endl;
	cout << "2. Calculate The Area of a Rectangle ." << endl;
	cout << "3. Calculate The Area of a Triangle. " << endl;
	cout << "4. Quit. " << endl;
	cout << "Please enter a choice (1-4) : " << endl;
	cin >> choice;

	if (choice >= 1 && choice <= 4);
	else if (cout << "You've entered an invalid selection.");
	
	{

		switch (choice)
		{

		case 1:

			cout << "What is the radius of the circle?. " << endl;
			cin >> radius;
			
				if (radius >= 1 && radius <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;			

			area = pie * pow(radius, 2);

			cout << "The area of the Circle is " << area << endl;
			break;
		
		case 2:

			cout << "Please enter the width. " << endl;
			cin >> width;
			
				if (width >= 1 && width <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;
			
			cout << "Please enter the height. " << endl;
			cin >> height;

			
				if (height >= 1 && height <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;
			
			area = width * height;

			cout << "The area of the Rectangle is " << area << endl;
			break;
		
		case 3:

			cout << "Please enter the base. ";
			cin >> base;

			
				if (base >= 1 && base <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;
			
			cout << "Please enter the height. " << endl;

			cin >> height;
			
				if (height >= 1 && height <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;
			
			area = .5 * base * height;

			cout << "This is the area of the Triangle. " << area << endl;
			break;

		case 4:
			cout << "Press any key to exit program." << endl;

		}
	}

	return 0;

}
You only need ONE switch keyword, then list your cases inside this.

Also your brace placement is pretty messed up.

You want to do (maybe) something like this:

I saw your example of where to place the brace's but, every time entered them VS 13 would place them there. Is that were the software wants them to be?
And thanks for the ONE switch keyword! Looks a bit better now!
Last edited on
And thanks for the ONE switch keyword! Looks a bit better now!

That has nothing to do with looking better. Having more than one switch is simply wrong :)


VS is normally pretty good with predicting where you want your closing brace to be.
Last edited on
So is the last code better than my first posting? Any more tips? I'll take all that you give, as I'm still learning haha
You've pretty much ignored all my advice about bracing :)

You still have stuff like this in it:

1
2
3
if (width >= 1 && width <= 1000);
				else if (cout << "You've entered an invalid selection.")
					break;


That semi colon at the end of the if should not be there. You want to also get into the habit of structuring if/else's like this:

1
2
3
4
5
6
7
8
9
10
11

if( blah)
{
       // do something
}
else
{
      // do something else
}




one more thing: this is the condition you want to calculate the area:

if (width >= 1 && width <= 1000)

but it looks like your code is treating this as the fail condition. If you look at my post again you'll see I swapped around the if/else conditions so the area is calculated if the user enters an integer between 1 and 1000, although i could be wrong in this. it's difficult to tell with your if/else structure :)
Last edited on
Gotcha!!! i see what you mean now! I'll correct the rest in the morning. Thanks Again!!
No worries dude :)
Topic archived. No new replies allowed.