473,573 Members | 3,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate user input in C++

11 New Member
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 6664
Banfa
9,065 Recognized Expert Moderator Expert
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
promiscuoustx
11 New Member
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
1509
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 that this input is an integer on the client sid In a diffrent grid I have an edit command column and the same problem - how do I make sure that this is...
5
3264
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 bound to a datagrid on the same page. It works 90% of the time as expected. However, for some unknown reason, I randomly get the error below. The...
0
976
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, e.Cancel is set to True. In this case Validate returns True or False correctly, based on whether the input was acceptable. But, if Validate is...
4
5755
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 inconclusive) ? cheers mark this is the error message
11
11747
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 in the validated event. Is there a way to know if all validated controls pass validation when the user clicks an OK button? In ASP.Net there's...
6
5216
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", "PRE", "DIV", "ADDRESS" start-tag. <input type = "hidden" name = "user" value = " "> <input type = "hidden" name = "talValue" value = "talValue">...
5
2560
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 which I validate as the user enters data, this is straight forward enough. Part of the validation ensures that all mandatory fields are entered before...
3
4294
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 received this error: Control 'cboCountry' referenced by the ControlToValidate property of
1
3981
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. Depends on the pull down list selection, I use script.aculo.us to validate the user input before submit and pass the necessary data, such as contact...
0
7661
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7977
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8165
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5252
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3692
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2163
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1256
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.