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