0

I've search the forums here and found a few other people with the same problem I have, but I'm not sure I understand the solutions that have been presented.

I have a simple geometry calculator I'm working on as C++ practice. My program displays a menu and asks for the user to input a number to select a function. When the user enters a character instead of a number the program loops endlessly.

Is there a simple way to catch the char input and ask the user to fix his choice, similar to the way my program handles a user entering a *number* that isn't in the menu?

```
#include <iostream>
#include <cmath>
using namespace std;
void wiseass();
void square();
void circle();
void triangle();
double
choice,
length,
radius,
area,
base,
height;
int main() {
//Introduce the program and get the user's choice
cout << "This is a simple geometry calculator.\n";
cout << "Would you like to find the area of a:\n";
cout << "(1) Square\n";
cout << "(2) Circle?\n";
cout << "or\n";
cout << "(3) Triangle?\n\n";
cout << "Please type the number that corresponds to your selection => ";
cin >> choice;
//Give the wiseass a chance to get his selection right
while (choice != 1 && choice != 2 && choice != 3 && choice !=42)
{
wiseass(); //call wiseass function
}
// (1) Call function for area of square
if (choice == 1)
{ square(); }
// (2) Call function for area of circle
else if (choice == 2)
{ circle(); }
// (3) Call function for area of triangle
else if (choice == 3)
{ triangle(); }
//Easter egg
else if (choice == 42)
{
cout << "\n42 is the ultimate answer to life, the universe, and everything. Since you already possess this answer, you obviously don't need this calculator. Goodbye!";
}
cout << "\n\n<END PROGRAM>\n\n";
return 0;
}
//###############################
//# When Dealing with a wiseass #
//###############################
void wiseass()
{
//If he enters a negative number
if (choice < 1)
{
cout << "\n\nThese are POSITIVE numbers, as in, GREATER than zero. Try it again...\n\n";
}
//If he enters a number not in the menu
else if (choice > 3 && choice != 42)
{
cout << "\nTry entering a number that's actually in the menu...\n\n";
}
//Display the menu again
cout << "Would you like to find the area of a:\n";
cout << "(1) Square\n";
cout << "(2) Circle?\n";
cout << "or\n";
cout << "(3) Triangle?\n\n=> ";
cin >> choice;
}
//##################################
//# (1) Solve the area of a square #
//##################################
void square()
{
cout << "\n\nPlease enter the length of the sides => ";
cin >> length;
//solve
area=length*length;
//Display result
cout << "\nThe area of your square is: " << area << endl;
}
//##################################
//# (2) Solve the area of a circle #
//##################################
void circle()
{
cout << "\n\nPlease enter the radius of your circle => ";
cin >> radius;
//solve
area=pow(3.14*radius, 2);
//Display result
cout << "\nThe area of your circle is: " << area << endl;
}
//####################################
//# (3) Solve the area of a triangle #
//####################################
void triangle()
{
cout << "\nPlease enter the length of your triangle's base => ";
cin >> base;
cout << "\nPlease enter your triangle's height => ";
cin >> height;
//solve
area=(base*height)/2;
//Display result
cout << "\nThe area of your triangle is: " << area << endl;
}
```

Be gentle. I'm a noob. ;(