Need help on improving my calculator program

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;
}
Last edited on
I believe when you said "14" you mean "1-4".

Do a for statement like this :for(bool retry = false; retry;)
You can place this just before the couts, or even farther back to make thous variables have a smaller scope(but really, its unnecessary).

When you get a wrong value / incorrect choice, do: retry = true;

And that's it.
I hope you understand without showing all the code, because this is really basic, read a book.
I tried putting the statement at the end of cout but then i got lot of error. Where do you put it exactly???
...
before the couts, and make the braces go all the way before the return 0.
can you please write out the whole code please... this things give me a headache because i never learn about that stuff in class yet....
Topic archived. No new replies allowed.