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;
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: