Ending the program on cin?

I am trying to create a simple data base for both mathematical and scientific formulas and I am just at the beginning. When I am trying to get the user to input a numerical value for a variable, the program closes as soon a number has been entered the program closes down. Any suggestion on why this happens would be nice. Including the code for it below.


#include <cmath>
#include <iostream>
#include <limits>


using namespace std;

int main()
{
// Create neccisary variables.
int x;

// Intro into the program
cout << "******************* Welcome to the formula data base.*******************" << endl << endl
<< "Please choose from the math or science category by entering the appropriate"<< endl << " number."
<< endl << " 1.Math 2.Science" << endl;

cin >> x;

cout << endl << endl;

if (x==1)
cout << "Mathematics" << endl << endl;
else if (x==2)
cout << "Science" << endl << endl;
else
cout << "Unrecognized key. Please reenter the appropriate number, based on your specific need." << endl << main();

std::cout << endl<< endl << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return (0);
}
closed account (18hRX9L8)
getchar();
or
cin.ignore();
Any elaboration on that? I included the cin.ignore so the program would run in the first place. I got it from one of the first posts on the beginners forum.
Topic archived. No new replies allowed.