468,513 Members | 1,854 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Validate user input in C++

Hi again all!! I am trying to write my program below to validate user input. In other words, if the user inputs something like "1a2.34", I need to return a message that states invalid data and for them to retry. I am really lost on this one, and I really need some help. Thanks so much!!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>        
  2. #include <iomanip>
  3. #include <cmath>    
  4. #include <cctype>
  5. #include <string>
  6. #include <sstream>    
  7.  
  8.  
  9. using namespace std;
  10. int main()
  11.  
  12. {
  13.      double a; //This is the amount of the mortgage the user must enter
  14.      double i; //This is the interest rate the user must enter
  15.      int y; //This is the amount of years for the mortgage the user must enter            
  16.      double mPayment; //This is a variable for ouputting the payment            
  17.      char YesNo = 'Y';
  18.  
  19.  
  20. do //In order to allow the user to be able to re-enter data, we must have a loop
  21.  
  22. {
  23.     cout << "What is the amount of the mortgage?  For example 200000" << endl;
  24.     cout << "Press enter." << endl;
  25.     cin    >>    a;    
  26.     cout << endl;
  27.     cout << "What is the amount of years the mortgage will be financed? For example 30" << endl;
  28.     cout << "Press enter." << endl;
  29.     cin >>    y;
  30.     cout << endl;
  31.     cout << "What is the interest rate? For example 5.75" << endl;
  32.     cout << "Press enter." << endl;
  33.     cin >>    i;
  34.     cout << endl;
  35.  
  36. //These are the variables required to calculate the information the user inputs
  37. double monInterest = i / 12 / 100; //Calcualtes the interest monthly        
  38. int t = y * 12; //This is the loan term in the amount of months
  39.  
  40. //This is the actual formula for calculating the mortgage payment amount 
  41. mPayment = (a * monInterest) / (1-pow((1+monInterest),-t));
  42.  
  43. //This allows the user to view what they entered and also what the monthly payment would be
  44.     cout << "Amount of mortgage = $" << a << endl;
  45.     cout << "Year financed = " << i << "%" << endl;
  46.     cout << "Interest Rate = " << y << " years" << endl;
  47.     cout << endl;
  48.     cout << "Monthly Payment Amount = $" << mPayment << endl;
  49.     cout << endl;
  50.  
  51.  
  52. //This allows the user to either enter in new information, or exit the program
  53.     cout << "If you would like to enter different information, please press Yes." << endl;
  54.     cout << "If you would like to exit this program, please press No." << endl;
  55.     cout << endl;
  56.     cin >> YesNo;
  57. }
  58.  
  59. while ((YesNo == 'Y') || (YesNo == 'y')); //End of the loop    
  60.  
  61.     return 0;
  62.  
  63. }
  64.  
Sep 10 '06 #1
2 6398
Banfa
9,056 Expert Mod 8TB
If you are trying to validate user input then you can not use code like this

Expand|Select|Wrap|Line Numbers
  1.   double a;
  2.  
  3.   cin >> a;
  4.  
because you have let C++ do all the data handling for you internally, if you input something like

12b7

a will take the value 12

The only real way to properly validate user input is to read the whole line into a string and then parser that string for the data you expect and produce an error if you find anything different.
Sep 10 '06 #2
So should I change double a; to string a;? I am really lost on this.
Sep 10 '06 #3

Post your reply

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

Similar topics

1 post views Thread by mats | last post: by
reply views Thread by Marina | last post: by
4 posts views Thread by Mike Fellows | last post: by
11 posts views Thread by jjbutera | last post: by
6 posts views Thread by Ed Jay | last post: by
3 posts views Thread by Max2006 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.