By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
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
  1. // Interest calculator, by Bl00dFox
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. float r;
  7. float p;
  8. float t;
  9. float i;
  10. float type;
  11.  
  12. int title ()
  13. {
  14. cout << "**************************************************************" << endl;
  15. cout << "Interest Calculator, by Bl00dFox" << endl;
  16. cout << "**************************************************************" << endl;
  17. }
  18.  
  19. int main ()
  20. {
  21. title();
  22. cout << endl;
  23. cout << "Compound or simple interest?" << endl;
  24. cout << "Select 1 for simple interest, select 2 for compund interest: ";
  25. cin >> type;
  26.  
  27. if (type == 1)
  28. {
  29. cout << endl;
  30. cout << "Enter the interest rate (in percentage per annum): ";
  31. cin >> r;
  32. cout << "How much money are you investing? $";
  33. cin >> p;
  34. cout << "How long are you investing (in years)? ";
  35. cin >> t;
  36. cout << endl;
  37. i = t * p * (r/100);
  38. cout << "Interest made: $" << i << endl;
  39. cout << "Total balance: $" << i + p << endl;
  40. system("PAUSE");
  41. return 0;
  42.  
  43. }
  44.  
  45. if (type == 2)
  46. {
  47. cout << endl;
  48. cout << "Enter the interest rate (in percentage per annum): ";
  49. cin >> r;
  50. cout << "How much money are you investing? $";
  51. cin >> p;
  52. cout << "How long are you investing (in years)? ";
  53. cin >> t;
  54. cout << endl;
  55. i =  ((pow((1+(r/100)), t)) * p) - p;
  56. cout << "Interest made: $" << i << endl;
  57. cout << "Total balance: $" << i + p << endl;
  58. system("PAUSE");
  59. return 0;   
  60.  
  61. }
  62. else
  63. {
  64. cout << endl;
  65. cout << "Please enter either 1 or 2" << endl;
  66. cout << "Program will now restart..." << endl;
  67. cout << endl;
  68. main();
  69. return 0; 
  70.  
  71.  
  72.  
  73. }
  74.  

Thanks in advance guys
Mar 19 '07 #1
Share this Question
Share on Google+
1 Reply


DeMan
100+
P: 1,806
I would use a while loop viz:

Expand|Select|Wrap|Line Numbers
  1. while((type != 1) && (type !=2)) // you may need quotes here...I'm never sure
  2. {
  3.   cout << endl;
  4.   cout << "Compound or simple interest?" << endl;
  5.   cout << "Select 1 for simple interest, select 2 for compund interest: ";
  6.   cin >> type;
  7. }
  8.  
And set type initially to something other than 1 or 2
Mar 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.