By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,085 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

validating the comma separated value message in C++

P: n/a
I want to know how to validate a string which is comma separated
"C1","2","12344","Mr","John","Chan","05/07/1976".........
I need to validate each field value against a set of rules, for example
checking if the string is a character or a numeric value, for the
length of the character etc.

Jul 4 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Nishanth wrote:
I want to know how to validate a string which is comma separated
"C1","2","12344","Mr","John","Chan","05/07/1976".........
I need to validate each field value against a set of rules, for example
checking if the string is a character or a numeric value, for the
length of the character etc.
Separate the string into the fields, and then compare each subfield
against the validation criteria. The remainder is left as an exercise
for the reader.
Jul 4 '06 #2

P: n/a
Hi Red,

Can you be more specific. as i am a newbie to c++ programming.
How do I separate the string into the fields? and how do I validate
each of them?

If you can help me out with some example programmes or some websites
which help out begginners with this it will be quite help full.

Thanks,
Nishanth

red floyd wrote:
Nishanth wrote:
I want to know how to validate a string which is comma separated
"C1","2","12344","Mr","John","Chan","05/07/1976".........
I need to validate each field value against a set of rules, for example
checking if the string is a character or a numeric value, for the
length of the character etc.
Separate the string into the fields, and then compare each subfield
against the validation criteria. The remainder is left as an exercise
for the reader.
Jul 4 '06 #3

P: n/a
Hi Red,

Can you be more specific. as i am a newbie to c++ programming.
How do I separate the string into the fields? and how do I validate
each of them?

If you can help me out with some example programmes or some websites
which help out begginners with this it will be quite help full.

Thanks,
Nishanth

red floyd wrote:
Nishanth wrote:
I want to know how to validate a string which is comma separated
"C1","2","12344","Mr","John","Chan","05/07/1976".........
I need to validate each field value against a set of rules, for example
checking if the string is a character or a numeric value, for the
length of the character etc.
Separate the string into the fields, and then compare each subfield
against the validation criteria. The remainder is left as an exercise
for the reader.
Jul 4 '06 #4

P: n/a

Nishanth wrote:
Hi Red,

Can you be more specific. as i am a newbie to c++ programming.
How do I separate the string into the fields? and how do I validate
each of them?

If you can help me out with some example programmes or some websites
which help out begginners with this it will be quite help full.
Hint :- Tokenize the source string by using ',' as token.

~Madhav

Jul 4 '06 #5

P: n/a
"Nishanth" <ra**********@gmail.comwrote:
I want to know how to validate a string which is comma separated
"C1","2","12344","Mr","John","Chan","05/07/1976".........
I need to validate each field value against a set of rules, for example
checking if the string is a character or a numeric value, for the
length of the character etc.
I'd recommend, first put your string in a std::string, so you
get all its cool member functions. (Research "std::string".)

Then, make a class with data members to hold your fields and a
parameterized constructor to parse the input string into its
fields and store the fields in the members.

I'm guessing from looking at your sample string that you're not
allowing any embedded spaces in your fields? If so, that
makes things simple, because you can just convert all the quote
marks and commas into spaces, then use a stringstream and the
">>" operator to dump the fields into your members.

Something like this should work for you:

#include <iostream>
#include <string>
#include <sstream>

using std::cout;
using std::endl;

class Dossier
{
public:
// parameterized constructor:
Dossier(std::string InputString);
// declare validation functions here
// declare any other methods you need here
// You might want to consider making these private,
// but I'm leaving them public for now, for simplicity:
std::string code;
std::string number;
std::string zip;
std::string title;
std::string first_name;
std::string last_name;
std::string dob;
};

Dossier::Dossier(std::string InputString)
{
std::replace(InputString.begin(), InputString.end(), '\"', ' ');
std::replace(InputString.begin(), InputString.end(), ',' , ' ');
std::istringstream SS(InputString);
SS >code >number >zip >title >first_name >last_name >dob;
}

int main()
{
std::string argle =
"\"C1\",\"2\",\"12344\",\"Mr\",\"John\",\"Chan\",\ "05/07/1976\"";
Dossier d(argle);
cout
<< d.code << endl
<< d.number << endl
<< d.zip << endl
<< d.title << endl
<< d.first_name << endl
<< d.last_name << endl
<< d.dob << endl;
return 0;
}
It should be a simple matter to add whatever validation
functions you want, as member functions of your class.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 4 '06 #6

P: n/a
Robbie,

I copied the above said programme and tried running it in VC++, I got
an error as
'replace' : is not a member of 'std' & 'replace' : undeclared
identifier.

I have not made any changes to the code.

Please help.

Thanking you,
Nishanth

Jul 4 '06 #7

P: n/a
TB
Nishanth skrev:
Robbie,

I copied the above said programme and tried running it in VC++, I got
an error as
'replace' : is not a member of 'std' & 'replace' : undeclared
identifier.

I have not made any changes to the code.

Please help.
#include <algorithm>

--
TB @ SWEDEN
Jul 4 '06 #8

P: n/a
"Nishanth" writes:
I copied the above said programme and tried running it in VC++, I got
an error as
'replace' : is not a member of 'std' & 'replace' : undeclared
identifier.

I have not made any changes to the code.
You are expected to learn something from this process. Look in your
reference material and see if Robbie misspelled "replace" or something such
as that. You should start building up some resources to supplement your
text book. For example:

http://www.dinkum.com/manuals/?manua...=algorith.html

shows that there is, indeed such a function. And that its prototype is in
<algorithms>. But Robbie simply forgot to include that header. Now try to
fix things up. Keep the above link where you can find it again. Don't be
shy about address pruning. If you don't know what pruning is, find out.
Jul 4 '06 #9

P: n/a

"Nishanth" <ra**********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>I want to know how to validate a string which is comma separated
"C1","2","12344","Mr","John","Chan","05/07/1976".........
I need to validate each field value against a set of rules, for example
checking if the string is a character or a numeric value, for the
length of the character etc.
This is called "Comma Separated Variables" or CSV. The easiest solution,
download the CSVParser class (google for it). It does not have every
possible variable type defined in it (such as your date) but you can add
those fairly easily.

If this is homework, however, you'll need to write your own parser.
Jul 4 '06 #10

P: n/a
On 3 Jul 2006 22:30:39 -0700 in comp.lang.c++, "Nishanth"
<ra**********@gmail.comwrote,
>How do I separate the string into the fields?
A splitter example is at:
http://groups.google.com/gr*********....earthlink.net
Jul 4 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.