Validate user input in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • promiscuoustx
    New Member
    • Sep 2006
    • 11

    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!!
    Code:
    #include <iostream>		
    #include <iomanip>
    #include <cmath>	
    #include <cctype>
    #include <string>
    #include <sstream>	
    		
    
    using namespace std;
    int main()
    				
    {
         double a; //This is the amount of the mortgage the user must enter
         double i; //This is the interest rate the user must enter
         int y; //This is the amount of years for the mortgage the user must enter			
         double mPayment; //This is a variable for ouputting the payment			
         char YesNo = 'Y';
    	
    	
    do //In order to allow the user to be able to re-enter data, we must have a loop
    
    {
    	cout << "What is the amount of the mortgage?  For example 200000" << endl;
    	cout << "Press enter." << endl;
        cin	>>	a;	
    	cout << endl;
    	cout << "What is the amount of years the mortgage will be financed? For example 30" << endl;
    	cout << "Press enter." << endl;
        cin >>	y;
    	cout << endl;
    	cout << "What is the interest rate? For example 5.75" << endl;
        cout << "Press enter." << endl;
        cin >>	i;
    	cout << endl;
    
    //These are the variables required to calculate the information the user inputs
    double monInterest = i / 12 / 100; //Calcualtes the interest monthly		
    int t = y * 12; //This is the loan term in the amount of months
    
    //This is the actual formula for calculating the mortgage payment amount 
    mPayment = (a * monInterest) / (1-pow((1+monInterest),-t));
    
    //This allows the user to view what they entered and also what the monthly payment would be
    	cout << "Amount of mortgage = $" << a << endl;
    	cout << "Year financed = " << i << "%" << endl;
    	cout << "Interest Rate = " << y << " years" << endl;
    	cout << endl;
    	cout << "Monthly Payment Amount = $" << mPayment << endl;
    	cout << endl;
    
    
    //This allows the user to either enter in new information, or exit the program
    	cout << "If you would like to enter different information, please press Yes." << endl;
    	cout << "If you would like to exit this program, please press No." << endl;
        cout << endl;
    	cin >> YesNo;
    }
    
    while ((YesNo == 'Y') || (YesNo == 'y')); //End of the loop	
    
    	return 0;
    
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    If you are trying to validate user input then you can not use code like this

    Code:
      double a;
    
      cin >> a;
    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.

    Comment

    • promiscuoustx
      New Member
      • Sep 2006
      • 11

      #3
      So should I change double a; to string a;? I am really lost on this.

      Comment

      Working...