Validating Data in C programming

G

gurdz

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. :)
 
M

MQ

gurdz said:
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...
 
F

Flash Gordon

gurdz said:
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.
 
R

Richard Heathfield

Flash Gordon said:

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.
 
L

lovecreatesbeauty

Richard said:
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.
 
M

MQ

Richard said:
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
 
M

Malcolm

Andrew Poelstra said:
...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;
}
 
A

Al Balmer

Flash Gordon said:



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.

<g> Looks like a left-over debug statement. A place to set a
breakpoint.
 
E

Eric Sosman

Andrew said:
...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?)
 
R

Richard Heathfield

Eric Sosman said:

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.
 
E

Eric Sosman

Richard said:
Eric Sosman said:




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.
 
G

gurdz

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
 
G

gurdz

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
 
G

Gordon Burditt

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>
 
B

Bill Pursell

Gordon said:
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. :)
 
A

Al Balmer

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.
 
M

MQ

gurdz said:
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.
 
R

Richard

MQ said:
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.
 
G

gurdz

MQ said:
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top