having trouble, how would i loop this?

i'm having some trouble figuring out how to loop this... i would like to loop until the user enters a valid input when validating the inputs. please help!! thanks.


{
int area_rec, radious, area, width, length, height;
float area_circ, area_tri;
const double PI = 3.14;
cout << "enter 1 for the radious of a circle" << endl;
cout << "enter 2 for the radious of a rectangle" << endl;
cout << "enter 3 for the radious of a triangle" << endl;
cout << "enter 4 to quiz program" << endl;

while (1==1)
{
cout << "choose your option : ";
cin >> area;
cout << endl;

switch (area)
{
case 1:
cout << "enter the radious of a circle \n";
cin >> radious;
cout << "enter the area of a circle\n";
area_circ = PI * radious * radious;
cin >> area_circ;
cout << "the area of the circle is " << area_circ << endl;
break;

case 2:
cout << "enter the length of the rectangle" << endl;
cin >> length;
cout << "enter the width of the rectangle" << endl;
cin >> width;
cout << "enter the area of the rectangle";
area_rec = length * width;
cout << "the area of the rectangle is " << endl;
break;

case 3:
cout << "enter the length of the triangle" << endl;
cin >> length;
cout << "enter the height of the traingle" << endl;
cin >> height;

area_tri = length * height /2;

cout << "The area of the triangle is " << area_tri << endl;

break;

case 4:
cout << "program quit" << endl;
return 0;
}
}
}
1. Global variables like const double PI should be declared outside main()
2. By choosing int as the data type for radius, width, length, height the program restricts itself unnecessarily; it is more flexible to choose double throughout
3. Go easy with the endls in your program: http://stackoverflow.com/questions/5492380/what-is-the-c-iostream-endl-fiasco/5492605#5492605
4. From your experience with this program you've probably noticed how tricky the cin and int combination is to do proper input validation in C++ (this page might be useful: http://www.learncpp.com/cpp-tutorial/185-stream-states-and-input-validation/). Therefore, as suggested in the previous link, I've used strings to enter and validate user entered data before saving them in int (for menu choice) or double (all other variables) via stringstream and an overloaded input_validator() function:
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
103
104
105
#include <iostream>
#include <sstream>
using namespace std;

const double PI = 3.14;
int input_validator();
double input_validator(const string& s);
int main()
{
    bool fQuit = false;
    while(!fQuit)
    {
        cout<<"1. Circle \n2. Rectangle \n3. Triangle \n4. Quit \n";
        int choice = input_validator();

        switch(choice)
        {
            case 1:
            {
                string inputString = "Enter circle radius: ";
                double radius = input_validator(inputString);
                cout<<"Area of circe: "<<PI * radius * radius<<"\n";
            }
            break;
            case 2:
            {
                string inputStringLength = "Enter length of rectangle: ";
                double length = input_validator(inputStringLength);
                string inputStringBreadth = "Enter breadth of rectangle: ";
                double breadth = input_validator(inputStringBreadth);
                cout<<"Area of rectangle: "<<length * breadth<<"\n";
            }
            break;
            case 3:
            {
                string inputStringLength = "Enter length of triangle: ";
                double length = input_validator(inputStringLength);
                string inputStringHeight = "Enter height of triangle: ";
                double height = input_validator(inputStringHeight);
                cout<<"Area of triangle: "<< 0.5* length * height<<"\n";
            }
            break;
            case 4:
                fQuit = true;
                cout<<"Goodbye \n";
                break;
            default:
                cout<<"Incorrect entry, try again \n";
                break;
        }
    }
}
int input_validator()
{
    int result;
    bool num_check = false;
    while(!num_check)
    {
        string num_str;
        cin>>num_str;
        bool bValid = true;
        for (size_t i = 0; i < num_str.size(); i++)
        if (!isdigit(num_str[i]))
        {
            cout<<"Invalid entry, try again  \n";
            bValid = false;
            break;
        }
        if(!bValid)
        {
            continue;
        }
        stringstream num_stream(num_str);
        num_stream>>result;
        num_check = true;
    }
    return result;
}
double input_validator(const string& inputString)
{
    double result;
    bool num_check = false;
    while(!num_check)
    {
        cout<<inputString<<"\n";
        string num_str;
        cin>>num_str;
        bool bValid = true;
        for (size_t i = 0; i < num_str.size(); i++)
        if (!isdigit(num_str[i])&&num_str[i]!= '.')
        {
            cout<<"Invalid entry, try again  \n";
            bValid = false;
            break;
        }
        if(!bValid)
        {
            continue;
        }
        stringstream num_stream(num_str);
        num_stream>>result;
        num_check = true;
    }
    return result;
}
Topic archived. No new replies allowed.