473,383 Members | 1,803 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,383 software developers and data experts.

validating the comma separated value message in C++

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
10 2654
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
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
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

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

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

Similar topics

7
by: Craig Keightley | last post by:
is it possible to compare acomma separated list aginst another eg comma list 1 => 1,2,3,4,5 comma list 2 => 3,5 can you check that 3 is in both, and 5 is in both, therfore they match??? the...
1
by: Craig Keightley | last post by:
I can do the match perfectly but what i also need to do is create a third list of comma separated values that are in both eg: List 1 => 1,2,3,4,5,6,7,8,11 List 2 => 1,3,4,5,6,7,10,23 ...
26
by: Christina | last post by:
I have a post-form that holds a listbox with mulitple selected items. When the form is posted to the server ASP file, I want to loop through the selected items, to insert each of them into a table....
2
by: Rahul Chatterjee | last post by:
Hello All I am uploading a file using ASP code. After the user uploads the file, I would like to be able to open the file and check if the data is in a given format (comma separated) and also...
11
by: Craig Keightley | last post by:
I have a mysql database with a list of companies who supply specific products tblSuppliers (simplified) sID | sName | goodsRefs 1 | comp name | 1,2,3,4,5 2 | company 2 | 2,4
7
by: ani | last post by:
I have two submit buttons in my form which need to validate two different controls on the page . How do I validate portions of the asp.net page. The two submit buttons should validate their...
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
4
by: sufian | last post by:
Below is the field where user enters his/her email address and the AJAX post request is sent to the server and the user sees the message: echo("<div id=\"message\" class=\"success\">Thank you! You...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.