How to improve my geometry calculator in the 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.
● Also avoid infinite loop

I also want to see how it work so can anyone please improvise and improve it with shown code please? how to avoid Here is what i have so far:

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
#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;
}
Before the cout << "Geometry Calculator\n";
do:
1
2
for(bool retry = false; retry;) //just putting in "retry" is equal to "retry == true"
{

and before the return put the close brace.

In every invalid input / choice, do retry = true;

basic man...

edit: and also, put retry = false; for the correct answers.
Last edited on
Registered users can post here. Sign in or register to post.