data validation

G

gurumare

How do i validate data? IE if a have a variable X of type int, and i
promt a user with cin >> X; how do i make sure they don't answer with a
letter? or a symbol?
 
P

Phlip

gurumare said:
How do i validate data? IE if a have a variable X of type int, and i
promt a user with cin >> X; how do i make sure they don't answer with a
letter? or a symbol?

Use std::getline(cin, myString) to pull a string. That lets the user hit
<Enter> to turn-around the input.

Then call:

char * end = NULL;
strtol(myString.c_str(), 10, &end);

Test 'end' to see if it truly points to myString.end(). That gives a quick,
simple, and robust validation.
 
V

Victor Bazarov

Phlip said:
Use std::getline(cin, myString) to pull a string. That lets the user hit
<Enter> to turn-around the input.

Then call:

char * end = NULL;
strtol(myString.c_str(), 10, &end);

Test 'end' to see if it truly points to myString.end(). That gives a
quick,
simple, and robust validation.

For one, I'd probably declared 'end' as const char*. And is it guaranteed
that the pointer returned by c_str() actually survives long enough for 'end'
to still be valid some time later when you check its value? I wonder how
well implementations handle this mix of std::string and std::strXXX library
functions especially for checking if 'end' "points to myString.end()". Are
you sure that c_str() returns a pointer whose last element coincides with
what 'std::string::end()' returns? I can't find any guarantee of it in the
Standard.

V
 
V

Victor Bazarov

Could you tell me where in the FAQ? a search came up with nada

I'd try section 15. However, all of it is useful information and
you should be at least familiar with what's there and what's not
there. So, start from the top and read.
 
G

gurumare

Thank you, i looked in section 15 and found exactly what i needed. I
searched the faq for 'validation' i guess what i needed was 'bad input'
once again thanks
 
R

Rolf Magnus

Could you tell me where in the FAQ? a search came up with nada

How did you search? It's quite hard to miss the chapter about "Input/output
via <iostream> and <cstdio>".
 
R

Rolf Magnus

Victor said:
For one, I'd probably declared 'end' as const char*. And is it guaranteed
that the pointer returned by c_str() actually survives long enough for
'end' to still be valid some time later when you check its value?

Yes. It lives as long as the whole expression.
I wonder how well implementations handle this mix of std::string and
std::strXXX library functions especially for checking if 'end' "points to
myString.end()". Are you sure that c_str() returns a pointer whose last
element coincides with what 'std::string::end()' returns? I can't find
any guarantee of it in the Standard.

You have a point here. It's not required that c_str() returns a pointer to
the original string content.
 
V

Victor Bazarov

Rolf said:
Victor Bazarov wrote:




Yes. It lives as long as the whole expression.

But I don't see the test of 'end' against myString.end() anywhere in this
full expression. Do you?

I think there is a guarantee that the pointer survives until the string is
actually modified. But I am not sure. Too lazy to look it up.

V
 
K

Kurt Stutsman

Victor said:
I think there is a guarantee that the pointer survives until the string is
actually modified. But I am not sure. Too lazy to look it up.

The value returned by c_str() is guaranteed to be valid until a non-const
method of string is called (C++ Standard, 21.3.6.2).
 
R

Rolf Magnus

Oops, didn't read it carefully enough. I thought we were talking about a
temporary string.
But I don't see the test of 'end' against myString.end() anywhere in this
full expression. Do you?

I think there is a guarantee that the pointer survives until the string is
actually modified. But I am not sure. Too lazy to look it up.

Yes.
 
G

gurumare

Ok, i have another question then, upon reading the FAQ, i found a great
way to differentaite between an int and a char in validation. But what
if you user enters 1a. the 1 makes it through my validation, and then
the a messes everything up. Is there a way to take care of that? I am a
student, and this is my first semester in c++ and i have a crafty
teacher who just may do that to see how well my validation is. thanks
 
V

Victor Bazarov

Kurt said:
The value returned by c_str() is guaranteed to be valid until a
non-const method of string is called (C++ Standard, 21.3.6.2).

So, if one calls the const variation of the 'end()' member function, then
one should be OK when comparing the pointer obtained from 'strtol' to it.
One would need to cast the string to 'const string' first, of course, to
achieve that. Otherwise, the non-const variation of 'end()' is called and
the pointer returned by 'c_str()' (and the one obtained from 'strtol') are
not valid any longer. Of course even in case of calling 'end() const' it
is still not guaranteed to produce the desired result due to the other
reason I mentioned. Thank you for clarifying the matter, though.

V
 
K

Karl Heinz Buchegger

Ok, i have another question then, upon reading the FAQ, i found a great
way to differentaite between an int and a char in validation. But what
if you user enters 1a. the 1 makes it through my validation, and then
the a messes everything up. Is there a way to take care of that? I am a
student, and this is my first semester in c++ and i have a crafty
teacher who just may do that to see how well my validation is. thanks

To quote a previous poster

strtol is kind enough to tell you where it stopped translating
the string to an integer. Either strtol has processed the whole
string (then the whole string was one single integer) or it
did not (in that case there are some non digit characters left
in the string)
 
V

Victor Bazarov

Ok, i have another question then, upon reading the FAQ, i found a great
way to differentaite between an int and a char in validation. But what
if you user enters 1a. the 1 makes it through my validation, and then
the a messes everything up. Is there a way to take care of that? I am a
student, and this is my first semester in c++ and i have a crafty
teacher who just may do that to see how well my validation is. thanks

'a' doesn't mess things up. '1' will be interpreted as the int you wanted
to input. The rest of the stream (what comes after a valid number) is
usually non-consequential. Of course, you could write a more clever
validation algorithm according to more complicated rules you are willing
to impose, but AFA I'm concerned "1a" string does contain a valid integer.

What you do after '1' has been extracted is up to you. You could try to
go back to reading and report invalid input or stop if the input has been
exhausted. You could simply ignore the rest of the input. You could read
the input as a string an analyze it char by char and only if the sequence
in its entirety represents a number (in your understanding), accept it,
and report an error otherwise. The choices are many.

BTW, 1a is a valid hexadecimal value. :)

V
 
V

Victor Bazarov

Karl said:
To quote a previous poster

BTW, it should be

long value = strtol(.., &end, 10);
strtol is kind enough to tell you where it stopped translating
the string to an integer. Either strtol has processed the whole
string (then the whole string was one single integer) or it
did not (in that case there are some non digit characters left
in the string)

The only way to do it is to store the pointer returned by 'c_str()' in
a variable and compare 'end' to its real end without touching the original
string:

char const *buffer = myString.c_str();
char const *end = buffer;
long value = strtol(buffer, &end, 10);
if (end != buffer + strlen(buffer))
{
// error, not all chars have been converted
}

Neither the "previous poster" nor anybody else specified this before.
However, it is necessary, AFAIK.

V
 
G

gurumare

what does "Test 'end' to see if it truly points to myString.end()." I
am new to this, and i am really trying to understand, but i have no
idea how to do it, or even what these lines of code mean. I have never
heard of strtol nor do i know what it does, the same goes for c_str().
and so far, any attempts i have made to implement the code i have found
here only results in errors i do not know how to fix. I probably
wouldn't even know it if a had it right. Please help
 
P

Phlip

When you reply with Google Groups, use Preview, then Edit, to get the
replied-to text. Then trim most of it out, leaving enough context that we
can tell to what you replied.
what does "Test 'end' to see if it truly points to myString.end()." I
am new to this, and i am really trying to understand, but i have no
idea how to do it, or even what these lines of code mean. I have never
heard of strtol nor do i know what it does, the same goes for c_str().

What did Google tell you about strtol, and std::string ? What do your
tutorial tell you?

What do the sample programs that came with your compiler tell you?
and so far, any attempts i have made to implement the code i have found
here only results in errors i do not know how to fix.

Post the code and the errors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top