Please help improve this geometry calculator

I need to improve my program in a following ways;
● When the user enters wrong input for radius, length, width, base, or height, the
program will continue to prompt the same corresponding error messages. Then the
program will ask the user to enter the input again.
● When the user enters the wrong choice for the calculator (other than 14).
The
program will prompt the following error “Please enter the correct choice (14)”,
and then it will ask the user to enter the choice again.
Below is my code, can anyone help me finish it? I have no idea of what to do first. I need to see the whole code to be able to understand it.

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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    const double PI = 3.14159;
    double radius=0.0;
    double area=0.0;
    double length=0.0;
    double width=0.0;
    double base=0.0;
    double height=0.0;
    int choice=0;

    cout << "Geometry Calculator\n";
    cout << "\n";
    cout << "1. Calculate the area of a Circle\n";
    cout << "2. Calculate the area of a Rectangle\n";
    cout << "3. Calculate the area of a Triangle\n";
    cout << "4. Quit\n\n";
    cout << "Enter your choice (1-4): ";
    cin >> choice;
    
    if (choice>=1 && choice<=4)
    {    
        switch (choice)
        {
            case 1: //Area of Circle
                cout << "\n";
                cout << "Enter the circle's radius: ";
                cin >> radius;
                
                if (radius>0)
                {    
                    area = PI * radius * radius;
                    //cout << fixed << setprecision(4);
                    cout << "The area is " << area;
                }
                else
                {
                    cout << "The radius can not be less than or equal zero.";
                }
                break;
            case 2: //Area of Rectangle
                cout << "\n";
                cout << "Enter the rectangle's length: ";
                cin >> length;
                cout << "Enter the rectangle's width: ";
                cin >> width;
                
                if (length>0 && width>0 && length>width)
                {    
                    area = length * width;
                    cout << "The area is " << area;
                }
                else
                {
                    cout << "\nOnly enter positive values for length and width and length must be greater than width.";
                }
            
                break;
            case 3: //Area of Triangle
                cout << "Enter the length of the base: ";
                cin >> base;
                cout << "Enter the triangle's height: ";
                cin >> height;
                
                if (base>0 && height>0)
                {    
                    area = (base * height)/2;
                    cout << "\nThe area is " << area;
                }
                else
                {
                    cout << "\nOnly enter positive values and real value for base and height.";
                }
                break;
            case 4:
                cout << "Thank you for using the geometry calculator.";
                break;
        }
    }
    else
    {
        cout << "The valid choices are 1 through 4. Run the\n program again and select one of those.";
    }

    return 0;
}
You need a do { ... } while(choice< 1 || choice > 4); loop the encompasses line 15 to 86
Can you put it in please ? as i have no reference for it
@Myna

I prefer to use a do { ... }while( choice !=4); loop.

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
// Geometry Calculator.cpp : main project file.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
 const double PI = 3.14159;
 double radius=0.0;
 double area=0.0;
 double length=0.0;
 double width=0.0;
 double base=0.0;
 double height=0.0;
 int choice=0;

 cout << "Geometry Calculator\n";

 do
 {
	cout << endl;
	cout << "1. Calculate the area of a Circle\n";
	cout << "2. Calculate the area of a Rectangle\n";
	cout << "3. Calculate the area of a Triangle\n";
	cout << "4. Quit\n\n";
	cout << "Enter your choice (1-4): ";
	cin >> choice;

	switch (choice)
	{
	case 1: //Area of Circle
	 cout << endl;
	 cout << "Enter the circle's radius: ";
	 cin >> radius;

	 if (radius>0)
	 {    
		area = PI * radius * radius;
		//cout << fixed << setprecision(4);
		cout << "The area of the circle is " << area << endl;
	 }
	 else
	 {
		cout << "The radius can not be less than or equal zero.";
	 }
	 break;
	case 2: //Area of Rectangle
	 cout << endl;
	 cout << "Enter the rectangle's length: ";
	 cin >> length;
	 cout << "Enter the rectangle's width: ";
	 cin >> width;

	 if (length>0 && width>0 && length != width) //  && length>width removed cause a rectangle can be wider than long
	 {    
		area = length * width;
		cout << "The area of the rectangle is " << area << endl;
	 }
	 if (length <= 0 || width <= 0) // Check that all entries were over 1 
	 {
		cout << endl << "Only enter positive values for length and width." << endl;
	 }
	 if (length == width) // // User enter a square, not rectangle
	 {
		cout << endl << "You entered the values of a square, NOT a rectangle. Try again, please.." << endl;
	 }

	 break;
	case 3: //Area of Triangle
	 cout << "Enter the length of the base: ";
	 cin >> base;
	 cout << "Enter the triangle's height: ";
	 cin >> height;

	 if (base>0 && height>0)
	 {    
		area = (base * height)/2;
		cout << endl << "The area of this triangle is " << area << endl;
	 }
	 else
	 {
		cout << "\nOnly enter positive values and real value for base and height." << endl;
	 }
	 break;
	case 4:
	 cout << "Thank you for using the geometry calculator." << endl;
	 break;
	default:
	 cout << "Your valid choices are 1 through 4, ONLY." << endl;
	}
 }while ( choice != 4);

 return 0;
}
add in
line 14: tryAgain:
line 85: goto tryAgain; //after cout << ...;
Last edited on
Thank you, i just figured how to use do it out too lol. Also to avoiding infinite loop if you enter in non digit numbers. I added cin.clear() and cin.ignore() right after after cin>>input
Registered users can post here. Sign in or register to post.