445,852 Members | 2,240 Online
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

# Help with simple c++ Program

 P: 12 Hi I am making a simple program to calculate interest. At the beginning when the user has to pick 1 or 2 (to select simple or compound interest respectively), if the user enters a letter (eg, a) the program goes haywire (the main repeats over and over again). Is there a way to limit the user in entering only numbers, not letters or characters? Here is the code: Expand|Select|Wrap|Line Numbers // Interest calculator, by Bl00dFox   #include  using namespace std;   float r; float p; float t; float i; float type;   int title () { cout << "**************************************************************" << endl; cout << "Interest Calculator, by Bl00dFox" << endl; cout << "**************************************************************" << endl; }   int main () { title(); cout << endl; cout << "Compound or simple interest?" << endl; cout << "Select 1 for simple interest, select 2 for compund interest: "; cin >> type;   if (type == 1) { cout << endl; cout << "Enter the interest rate (in percentage per annum): "; cin >> r; cout << "How much money are you investing? \$"; cin >> p; cout << "How long are you investing (in years)? "; cin >> t; cout << endl; i = t * p * (r/100); cout << "Interest made: \$" << i << endl; cout << "Total balance: \$" << i + p << endl; system("PAUSE"); return 0;   }   if (type == 2) { cout << endl; cout << "Enter the interest rate (in percentage per annum): "; cin >> r; cout << "How much money are you investing? \$"; cin >> p; cout << "How long are you investing (in years)? "; cin >> t; cout << endl; i =  ((pow((1+(r/100)), t)) * p) - p; cout << "Interest made: \$" << i << endl; cout << "Total balance: \$" << i + p << endl; system("PAUSE"); return 0;      } else { cout << endl; cout << "Please enter either 1 or 2" << endl; cout << "Program will now restart..." << endl; cout << endl; main(); return 0;    }      }   Thanks in advance guys Mar 19 '07 #1