Data/Type Validation

Completed Posted May 28, 2015 Paid on delivery
Completed 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.

C++ Programming

Project ID: #7756718

About the project

16 proposals Remote project Active May 31, 2015

Awarded to:

bencubeben

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

$20 USD in 1 day
(41 Reviews)
4.6

16 freelancers are bidding on average $23 for this job

billinhit

Hi I am an C++ code. I have over 6 years of experience on C++. Let me complete this project Thanks, Billinhit

$30 USD in 1 day
(13 Reviews)
4.9
Venky117

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

$25 USD in 1 day
(23 Reviews)
4.6
gbailey1

Hello, as a senior CS major at my university I understand what is expected for your assignment. I've done these kind of things in classes before and would have no problem with this. What I would do: The assignment More

$15 USD in 2 days
(9 Reviews)
4.0
hoangduong97

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

$25 USD in 1 day
(21 Reviews)
3.4
rajafaizan

A proposal has not yet been provided

$25 USD in 1 day
(7 Reviews)
3.6
BSODM

I'm a professional C++ programmer with big experience in this field and believe that can do the job for best time.

$13 USD in 0 days
(9 Reviews)
3.5
mcmbuvi

Hi I am an experienced C/C++/Java developer with excellent software engineering skills. I would like to work on the network security assignment and assure to deliver high quality solutions. My work experience inclu More

$25 USD in 1 day
(2 Reviews)
2.0
kailashnva

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

$20 USD in 1 day
(0 Reviews)
0.0
krutivyas

A proposal has not yet been provided

$25 USD in 1 day
(0 Reviews)
0.0
ducvo

A proposal has not yet been provided

$25 USD in 1 day
(0 Reviews)
0.0
desaijeet

Hello Sir/Ma'am, I would like to be considered for your project. I am new to this site and hence you can see that I accomplished only with one project. I have developed a major project in C++ and also have a pro More

$20 USD in 1 day
(1 Review)
0.0
risersit

A proposal has not yet been provided

$25 USD in 1 day
(0 Reviews)
0.0
vw7311560vw

A proposal has not yet been provided

$35 USD in 1 day
(0 Reviews)
0.0
kishbabu

Hi, I am Kishore having 9yrs of good exp with c++.. Thanks for viewing my proposal. Regards, Kishore

$25 USD in 1 day
(0 Reviews)
0.0
muhammadibrar99

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.

$15 USD in 1 day
(0 Reviews)
0.0