Data/Type Validation
$10-30 USD
Paid on delivery
How can you validate that the user entered the correct type of data? There are a few different possibilities involving regular expressions, exception handling or various cin functions, but for this assignment you will try a more basic approach. It involves more typing, but it illustrates the problem that the other approaches would be solving behind the scenes.
Anything the user types can be considered part of a valid string, so what we can do is read the input as a string (you would want to read the whole input - not just up to the first whitespace character) and then iterate through the string looking at the characters to determine whether the input is valid for the expected type (you may find the isdigit() function helpful). Once you verify that it is of the correct type, then you can convert it from a string to an actual int or floating point value. The version of C++ we are using does not have functions for converting C++ strings directly into those numeric types, but it does have a function in the string class called c_str() that converts a C++ string into a C-style string. From there we can use atoi() or atof() to convert the C-style string to an int or float respectively. If it turns out that the input string does not represent a valid value for the expected type, then you can loop back and ask the user to try again.
For this project, you will write two functions: one named stringIsInt (string is int) and one named stringIsFloat (string is float). stringIsInt() should take a C++ string parameter and return true if that string represents a valid int value and false otherwise. stringIsFloat() should take a C++ string parameter and return true if that string represents a valid floating-point value and false otherwise. Such functions could then be used in the process described above.
a string represents a valid int value if and only if it consists of (at most one) optional sign character (+ or -) followed by a sequence of (at least one) decimal digits. Spaces at the front or end of the string should have no effect, nor should spaces between a sign character and the first digit, but spaces between digits are not allowed. No other characters are allowed (this is just for decimal values).
a string represents a valid floating-point value if and only if it consists of (at most one) optional sign character (+ or -) followed by a sequence of (at least one) decimal digits, optionally containing at most one decimal point. The decimal point may appear before any digits, but must be followed by at least one digit. Spaces at the front or end of the string should have no effect, nor should spaces between a sign character and the first digit (or decimal point if it comes before any digits), but spaces between digits or between a digit and a decimal point are not allowed. No other characters are allowed (this is just for decimal values).
There are a number of cases to consider, and covering all of them correctly may seem tedious, but the great thing about a function is that you only have to code it once.
The file must be called: validation.cpp.
The python testing code and main method to test the validation methods are in the attachment. To use the testing code, type "python [login to view URL]" and a perfectly working code should score a 50/50.
Project ID: #7756718
About the project
Awarded to:
Hello, I am a Software Engineer and a competitive programmer and I have been using C and C++ for all my projects and coding contests. I currently rank 353 out of almost 38000 competitive programmers. Link to More
16 freelancers are bidding on average $23 for this job
Hi I am an C++ code. I have over 6 years of experience on C++. Let me complete this project Thanks, Billinhit
hi... i can do this. Please let me know to start immediately. i can deliver you on time. Please initiate communication to discuss further. thanks
Hello, I am experienced and specialized in C and C++ programing. I have taken part in many competitions, including national competition, in which I won 2nd prize. I always work with responsibility and quality. Please c More
I'm a professional C++ programmer with big experience in this field and believe that can do the job for best time.
I can work on this project. I have 4 years of experince in C++ programming. Award me the bid and will deliver the project in 1 day
Hi, I am Kishore having 9yrs of good exp with c++.. Thanks for viewing my proposal. Regards, Kishore
I can do this its not a problem for me i have done many projects in C/C++. I understand your problem and i can meet deadlines.