validating the comma separated value message in C++

N

Nishanth

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

red floyd

Nishanth said:
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.
 
N

Nishanth

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
 
N

Nishanth

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
 
M

MadhavC

Nishanth said:
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
 
R

Robbie Hatley

Nishanth said:
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/
 
N

Nishanth

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
 
T

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

osmium

Nishanth said:
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/?manual=compleat&page=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.
 
J

Jim Langston

Nishanth said:
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.
 

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
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top