468,512 Members | 1,382 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Validating Data in C programming

Does anyone know how to perform data validation in C? I have searched
google for every possible result, and I either end up with data
validation for C++ or nothing at all. I have also searched websites
such as planet-source-code.com, but my search has been in vain.

Can someone please help me out. Or at least, point me in the right
direction?

Thank you very much. :)

Aug 5 '06 #1
21 3991
MQ

gurdz wrote:
Does anyone know how to perform data validation in C? I have searched
google for every possible result, and I either end up with data
validation for C++ or nothing at all. I have also searched websites
such as planet-source-code.com, but my search has been in vain.

Can someone please help me out. Or at least, point me in the right
direction?

Thank you very much. :)
Data validation is a generic programming technique that is language
independant. It is just the process of checking that data input to
your program is within the range of expected values, etc. If you are
expecting a number between 1 and 10, check that it is a number between
1 and 10 before using it. What is so hard about that? Unless I am not
understanding your question...

Aug 5 '06 #2
gurdz wrote:
Does anyone know how to perform data validation in C?
Yes, you look at the data and see if it matches your validation rules.
I have searched
google for every possible result, and I either end up with data
validation for C++ or nothing at all. I have also searched websites
such as planet-source-code.com, but my search has been in vain.

Can someone please help me out. Or at least, point me in the right
direction?
It really depends on what the data you are trying to validate is, what
the rules are, and where the data is coming from. There is no one size
fits all solution.
--
Flash Gordon
Still sigless on this computer.
Aug 5 '06 #3
Flash Gordon said:

<snip>
>
It really depends on what the data you are trying to validate is, what
the rules are, and where the data is coming from. There is no one size
fits all solution.
Yes, there is.

The following fragment is taken from the source code used by a major UK
bank, the identity of which will remain anonymous (to protect the guilty).
It was spotted during Y2K work in the late 1990s:

if(d != 6)
{
d = 6;
}

Viola! All numbers are 6, even if they aren't. QED.

How we laughed! We laughed and laughed and laughed, until we stopped.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 5 '06 #4

Richard Heathfield wrote:
The following fragment is taken from the source code used by a major UK
bank, the identity of which will remain anonymous (to protect the guilty).
It was spotted during Y2K work in the late 1990s:

if(d != 6)
{
d = 6;
}

Viola! All numbers are 6, even if they aren't. QED.

How we laughed! We laughed and laughed and laughed, until we stopped.
It is possible that there are occasions for using it.

Aug 5 '06 #5
MQ

Richard Heathfield wrote:
The following fragment is taken from the source code used by a major UK
bank, the identity of which will remain anonymous (to protect the guilty).
It was spotted during Y2K work in the late 1990s:

if(d != 6)
{
d = 6;
}
I don't think that it is any secret that banks have their own brand of
logic, that seems to err in their favour

Aug 5 '06 #6
On 2006-08-05, lovecreatesbeauty <lo***************@gmail.comwrote:
>
Richard Heathfield wrote:
>The following fragment is taken from the source code used by a major UK
bank, the identity of which will remain anonymous (to protect the guilty).
It was spotted during Y2K work in the late 1990s:

if(d != 6)
{
d = 6;
}

Viola! All numbers are 6, even if they aren't. QED.

How we laughed! We laughed and laughed and laughed, until we stopped.

It is possible that there are occasions for using it.
....where typing `d = 6' or even just `6' wouldn't suffice?

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 5 '06 #7
"Andrew Poelstra" <ap*******@false.sitewrote
On 2006-08-05, lovecreatesbeauty <lo***************@gmail.comwrote:
>>
Richard Heathfield wrote:
>>The following fragment is taken from the source code used by a major UK
bank, the identity of which will remain anonymous (to protect the
guilty).
It was spotted during Y2K work in the late 1990s:

if(d != 6)
{
d = 6;
}

Viola! All numbers are 6, even if they aren't. QED.

How we laughed! We laughed and laughed and laughed, until we stopped.

It is possible that there are occasions for using it.

...where typing `d = 6' or even just `6' wouldn't suffice?
if(d != 6)
{
fprintf(stderr, "somethign fishy here\n");
d = 6;
}

if(d != 6)
{
// fprintf(stderr, "somethign fishy here\n");
d = 6;
}

Three months later, another compiler
Warning, non-standard comment line 3456

if((d != 6)
{
d = 6;
}

--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.

Aug 5 '06 #8
On Sat, 05 Aug 2006 10:18:58 +0000, Richard Heathfield
<in*****@invalid.invalidwrote:
>Flash Gordon said:

<snip>
>>
It really depends on what the data you are trying to validate is, what
the rules are, and where the data is coming from. There is no one size
fits all solution.

Yes, there is.

The following fragment is taken from the source code used by a major UK
bank, the identity of which will remain anonymous (to protect the guilty).
It was spotted during Y2K work in the late 1990s:

if(d != 6)
{
d = 6;
}

Viola! All numbers are 6, even if they aren't. QED.

How we laughed! We laughed and laughed and laughed, until we stopped.
<gLooks like a left-over debug statement. A place to set a
breakpoint.

--
Al Balmer
Sun City, AZ
Aug 5 '06 #9
Andrew Poelstra wrote:
On 2006-08-05, lovecreatesbeauty <lo***************@gmail.comwrote:
>>Richard Heathfield wrote:
>>>The following fragment is taken from the source code used by a major UK
bank, the identity of which will remain anonymous (to protect the guilty).
It was spotted during Y2K work in the late 1990s:

if(d != 6)
{
d = 6;
}

Viola! All numbers are 6, even if they aren't. QED.

How we laughed! We laughed and laughed and laughed, until we stopped.

It is possible that there are occasions for using it.


...where typing `d = 6' or even just `6' wouldn't suffice?
It's an optimization. On a virtual memory system, it avoids
setting the "dirty" bit for a memory page unnecessarily, thereby
avoiding the (potential) expense of a page-out operation later,
or at least not incurring the expense for no good purpose.

(Hmmm: That variable name, `d'. Was the author, by any
chance, named Reginald?)

--
Eric Sosman
es*****@acm-dot-org.invalid

Aug 5 '06 #10
Eric Sosman said:

<snip>
>
It's an optimization. On a virtual memory system, it avoids
setting the "dirty" bit for a memory page unnecessarily, thereby
avoiding the (potential) expense of a page-out operation later,
or at least not incurring the expense for no good purpose.

(Hmmm: That variable name, `d'. Was the author, by any
chance, named Reginald?)
I don't think it would be fair to reveal the source of the source, so to
speak.

Your optimisation explanation is ingenious, but my recollection of the code
base leaves me somewhat unconvinced that it is a correct explanation of the
motivation for this particular line.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 5 '06 #11
Richard Heathfield wrote:
Eric Sosman said:

<snip>
> It's an optimization. On a virtual memory system, it avoids
setting the "dirty" bit for a memory page unnecessarily, thereby
avoiding the (potential) expense of a page-out operation later,
or at least not incurring the expense for no good purpose.

(Hmmm: That variable name, `d'. Was the author, by any
chance, named Reginald?)


I don't think it would be fair to reveal the source of the source, so to
speak.

Your optimisation explanation is ingenious, but my recollection of the code
base leaves me somewhat unconvinced that it is a correct explanation of the
motivation for this particular line.
Must have been good old Reginald K. Denktash, then.

--
Eric Sosman
es*****@acm-dot-org.invalid

Aug 5 '06 #12
Well, I'm sorry if I got each and every person confused about data
validation, but actually, this question was raised because of my Data
Structures and Algorithms lecturer. She didn't teach us nuts about
validation, and she comes to class saying that we all have to do "data
validation for input".

I hope you all don't mind giving me some advice on this. I usually
don't ask people for this sort of help for assignments, as some see it
as unfair, however, I am totally lost on this subject.

Thanks once again,
kind regards,
Gurdip

Aug 6 '06 #13
Well, I'm sorry if I got each and every person confused about data
validation, but actually, this question was raised because of my Data
Structures and Algorithms lecturer. She didn't teach us nuts about
validation, and she comes to class saying that we all have to do "data
validation for input".

I hope you all don't mind giving me some advice on this. I usually
don't ask people for this sort of help for assignments, as some see it
as unfair, however, I am totally lost on this subject.

Thanks once again,
kind regards,
Gurdip

Aug 6 '06 #14
>Well, I'm sorry if I got each and every person confused about data
>validation, but actually, this question was raised because of my Data
Structures and Algorithms lecturer. She didn't teach us nuts about
validation, and she comes to class saying that we all have to do "data
validation for input".
Consider, for example, an input of a bet amount for a roulette game.
Chips cost $1 each.

If the bet is a valid number (e.g. contains no alphabetic characters),
and the bet is greater than zero and the bet is less than or equal
to the house limit and the bet is an integer and the bet is less
than or equal to the player's bank balance, then the bet amount is
valid.

Reminder: a great way to cheat at poorly-written gambling programs
is to bet a negative amount, using the house advantage against it,
then try to "lose".

Consider, for example, an input which is a telephone number. How
do you validate it?

Hint: isdigit(buffer[0]) returns 0 if the first character in buffer[]
is not a digit, and nonzero if it is.

Try 1: A valid telephone number consists only of digits.
User inputs: 1-800-555-1212
Try 2: A valid telephone number consists only of digits after removing
the characters -, (, ), and blanks.
User inputs: 1-800-555
Try 3: A valid telephone number consists of 10 digits after removing
the characters -, (, ), and blanks.
User inputs: 18005551212
Try 4: A valid telephone number consists of 10 digits after removing
the characters -, (, ), and blanks. If the input is 11 digits and
the first digit is a 1, remove it.
User inputs +44 1277 655112
Try 5: A valid USA telephone number consists of 10 digits after removing
the characters -, (, ), blanks, and the prefix 1 or 011, if present.
We don't care about the rest of the world.
User inputs: 18195551212
Try 6: A valid USA telephone number consists of 10 digits after removing
the characters -, (, ), blanks, and the prefix 1 or 011, if present.
Reject input whose first 3 digits after substitutions is a Canadian
or Mexican area code. Note that this code requires modification when a
new non-USA area code is assigned. We don't care about the rest of the world.
User inputs: 12145551212 Extension 234
<Programmer shoots User>
Aug 6 '06 #15

Gordon Burditt wrote:
Well, I'm sorry if I got each and every person confused about data
validation, but actually, this question was raised because of my Data
Structures and Algorithms lecturer. She didn't teach us nuts about
validation, and she comes to class saying that we all have to do "data
validation for input".

Consider, for example, an input of a bet amount for a roulette game.
Chips cost $1 each.

If the bet is a valid number (e.g. contains no alphabetic characters),
and the bet is greater than zero and the bet is less than or equal
to the house limit and the bet is an integer and the bet is less
than or equal to the player's bank balance, then the bet amount is
valid.

Reminder: a great way to cheat at poorly-written gambling programs
is to bet a negative amount, using the house advantage against it,
then try to "lose".

Consider, for example, an input which is a telephone number. How
do you validate it?

Hint: isdigit(buffer[0]) returns 0 if the first character in buffer[]
is not a digit, and nonzero if it is.

Try 1: A valid telephone number consists only of digits.
User inputs: 1-800-555-1212
Try 2: A valid telephone number consists only of digits after removing
the characters -, (, ), and blanks.
User inputs: 1-800-555
Try 3: A valid telephone number consists of 10 digits after removing
the characters -, (, ), and blanks.
User inputs: 18005551212
Try 4: A valid telephone number consists of 10 digits after removing
the characters -, (, ), and blanks. If the input is 11 digits and
the first digit is a 1, remove it.
User inputs +44 1277 655112
Try 5: A valid USA telephone number consists of 10 digits after removing
the characters -, (, ), blanks, and the prefix 1 or 011, if present.
We don't care about the rest of the world.
User inputs: 18195551212
Try 6: A valid USA telephone number consists of 10 digits after removing
the characters -, (, ), blanks, and the prefix 1 or 011, if present.
Reject input whose first 3 digits after substitutions is a Canadian
or Mexican area code. Note that this code requires modification when a
new non-USA area code is assigned. We don't care about the rest of the world.
User inputs: 12145551212 Extension 234
<Programmer shoots User>
This is a great analogy, but the final step is incorrect.
It should be: programmer shoots spec author. :)

Aug 6 '06 #16
On 5 Aug 2006 20:28:03 -0700, "gurdz" <gu*************@gmail.com>
wrote:
>Well, I'm sorry if I got each and every person confused about data
validation, but actually, this question was raised because of my Data
Structures and Algorithms lecturer. She didn't teach us nuts about
validation, and she comes to class saying that we all have to do "data
validation for input".
?
I really don't think any of the responders are confused:-)

Do you mean that you're still confused about it? Really, all it means
is that you must write code to make sure the data is valid
(reasonable, within specifications, etc.) before you use it. Suppose
you're calculating the average number of bus riders per run. You know
that the number on a given run can't be less than zero, and you know
that the bus holds only 66 riders. You check each input to make sure
that it's between 0 and 66. If it isn't you take some appropriate
action, like printing an error message.
>
I hope you all don't mind giving me some advice on this. I usually
don't ask people for this sort of help for assignments, as some see it
as unfair, however, I am totally lost on this subject.
This kind of help is OK. We won't write your program for you, though.
Once you have an honest attempt, post it here for a critique.

--
Al Balmer
Sun City, AZ
Aug 6 '06 #17
MQ

gurdz wrote:
Well, I'm sorry if I got each and every person confused about data
validation, but actually, this question was raised because of my Data
Structures and Algorithms lecturer. She didn't teach us nuts about
validation, and she comes to class saying that we all have to do "data
validation for input".

I hope you all don't mind giving me some advice on this. I usually
don't ask people for this sort of help for assignments, as some see it
as unfair, however, I am totally lost on this subject.

Thanks once again,
kind regards,
Gurdip
It is as simple as checking the data you have input. Say you are
writing a program that adds numbers together. You ask users to enter
the numbers to add. The users could type absolute shit into your
program, such as "kjshafdjh". You must check that it is a number, and
nothing else. THAT is data validation; extremely simple, nothing more,
nothing less.

Aug 6 '06 #18
"MQ" <mi**************@gmail.comwrites:
gurdz wrote:
>Well, I'm sorry if I got each and every person confused about data
validation, but actually, this question was raised because of my Data
Structures and Algorithms lecturer. She didn't teach us nuts about
validation, and she comes to class saying that we all have to do "data
validation for input".

I hope you all don't mind giving me some advice on this. I usually
don't ask people for this sort of help for assignments, as some see it
as unfair, however, I am totally lost on this subject.

Thanks once again,
kind regards,
Gurdip

It is as simple as checking the data you have input. Say you are
writing a program that adds numbers together. You ask users to enter
the numbers to add. The users could type absolute shit into your
program, such as "kjshafdjh". You must check that it is a number, and
nothing else. THAT is data validation; extremely simple, nothing more,
nothing less.
Type and Range.
Aug 6 '06 #19

MQ wrote:
gurdz wrote:
Well, I'm sorry if I got each and every person confused about data
validation, but actually, this question was raised because of my Data
Structures and Algorithms lecturer. She didn't teach us nuts about
validation, and she comes to class saying that we all have to do "data
validation for input".

I hope you all don't mind giving me some advice on this. I usually
don't ask people for this sort of help for assignments, as some see it
as unfair, however, I am totally lost on this subject.

Thanks once again,
kind regards,
Gurdip

It is as simple as checking the data you have input. Say you are
writing a program that adds numbers together. You ask users to enter
the numbers to add. The users could type absolute shit into your
program, such as "kjshafdjh". You must check that it is a number, and
nothing else. THAT is data validation; extremely simple, nothing more,
nothing less.
Hi, thanks for the response, and yes, I am looking for what you are
talking about. Can you please give me an example code to validate what
you mentioned above. I have been looking for exactly what you are
talking about, but no matter how I search Google or any other search
engine for that matter, I am unable to find the appropriate result. I
hope that you could please help me out.

Thanks for your kind assistance,
kind regards,
Gurdip

Aug 6 '06 #20
gurdz wrote:
MQ wrote:
>>
It is as simple as checking the data you have input. Say you are
writing a program that adds numbers together. You ask users to enter
the numbers to add. The users could type absolute shit into your
program, such as "kjshafdjh". You must check that it is a number, and
nothing else. THAT is data validation; extremely simple, nothing more,
nothing less.


Hi, thanks for the response, and yes, I am looking for what you are
talking about. Can you please give me an example code to validate what
you mentioned above. I have been looking for exactly what you are
talking about, but no matter how I search Google or any other search
engine for that matter, I am unable to find the appropriate result. I
hope that you could please help me out.
Some of the tests will depend on how you are obtaining
the input. Are you reading it from a file, or from an
interactive source, or getting it from the command-line
arguments, or what?

Suggestion: Just ignore the issue of validation (for the
moment; this is temporary) and write the program as if all
inputs were always trustworthy. Show us what you've written,
and people will suggest how you could fit some validation into
that framework.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 6 '06 #21
"gurdz" writes:

Hi, thanks for the response, and yes, I am looking for what you are
talking about. Can you please give me an example code to validate what
you mentioned above. I have been looking for exactly what you are
talking about, but no matter how I search Google or any other search
engine for that matter, I am unable to find the appropriate result. I
hope that you could please help me out.
Try Google groups instead of plain vanilla google.
Aug 6 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Iain | last post: by
1 post views Thread by Craig Beuker | last post: by
reply views Thread by Gary Shell | last post: by
4 posts views Thread by easoftware | last post: by
5 posts views Thread by ameen.abdullah | last post: by
3 posts views Thread by TheSteph | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.