The user can find a side, a hypotenuse, or to exit.

If I press a letter instead of a number my program breaks and the infinite loop never stops.

How do I make it that the user can use only numbers?

This is for my own knowledge, not homework!!!

Thank you!!

#include "stdafx.h" #include <iostream> #include <cmath> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { char in = 's'; 'x'; 'h'; // User choise. double s1, s2, h; do { cout << "Choose what to find: a side, a hypotenuse, or exit the program. [s/h/x]: "; // Prompt to user. cin >> in; switch (in){ case 'x': //To exit. cout << "Thank you for using my program! \n"; exit(0); break; case 'h': //To find hypotenous. cout << "To find the hypotenuse I want you to give me the two sides. \n"; cout << "Give me Side1: "; cin >> s1; cout << "Give me Side2: "; cin >> s2; if (s1 <= 0 || s2 <= 0) {cout << "Negative numbers and zero are not allowed. \n" <<endl; break;} else h=sqrt((s1*s1)+(s2*s2)); cout << "Your hypotenuse is: " << sqrt(h*h) <<endl <<endl; break; case 's': //To find side. cout << "To find the other side I want you to give me a side and your hypotenuse. \n"; cout << "Give me your side: "; cin >> s1; cout << "Give me your hypotenus: "; cin >> h; if (s1 <= 0 || h <= 0) {cout << "Negative numbers and zero are not allowed. \n" <<endl; break;} else s2 = sqrt((h*h)-(s1*s1)); cout << "Your other side is: " << sqrt(s2*s2) <<endl <<endl; break; } } while (1); return 0; }