473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 6645
Banfa
9,065 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

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

Similar topics

1
by: mats | last post by:
Hell My problem is that I do not know how to validate input that I let the user type into my datagrid In one grid I let the user to type in a value into a template column - how do I make sure...
5
by: sjl | last post by:
I've got an .aspx webform for searching my database. It basically takes user input and passes it as a parm into a stored proc to search a table. The results are returned in a SQLDataReader and...
0
by: Marina | last post by:
Let's say a user control has a bunch of textboxes. At some point the user control calls Validate to make sure all input is processed. The Validating event is fired, and if the input was invalid,...
4
by: Mike Fellows | last post by:
running IIS on a single server, hosting pages on an intranet basis, one single user out of 50 is having an unable to validate data issue how do i fix this (the microsoft KB is a little...
11
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider...
6
by: Ed Jay | last post by:
When I try to validate the following I receive three errors: Error Line 47 column 41: document type does not allow element "INPUT" here; missing one of "P", "H1", "H2", "H3", "H4", "H5", "H6",...
5
nathj
by: nathj | last post by:
Hi, I have been looking around the forum and the web for a way to achieve this and so far I have drawn a blank. So I head to the forum as I'm sure someone knows how to do this. I have a form...
3
by: Max2006 | last post by:
Hi, I have a user control that has a property named "Text" Is there any way that I use a RequiredFieldValidator or CompareValidator to validate my user control's value? I tried it and I...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.