473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 4456
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Iain | last post by:
I've got a system which takes an XML file, translates it into an update gram and then loads it into my database with SQLXML3 (all in dot net). But it's fragile. And the SQLXML 3 error reporting...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
5
by: norton | last post by:
Hi All, I am writing a simple win form which contains a button, when user click the button i want to validate all the other textbox and see if there is anything goes wrong may i know how...
4
by: easoftware | last post by:
I am using VS .Net 2003 and VB. I have an app with one parent and two Mdi child forms. I need to validate data in the Mdi form. The Form.Validating event works when I try to close a Mdi form,...
5
by: ameen.abdullah | last post by:
Hi Guys, I have a textbox in windows form that should only accept alphabets, numbers, spaces and underscore. If the textbox contains anyother character it should display a msg at the time of...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
8
by: Peted | last post by:
I have an amazing problem which i think i have no hope of solving Im working with a c# dot net module that is hosted by and runs under a delphi form envrioment. Dont ask me how this insanity has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.