I'm doing a project that reads some string reactions from file at formula e.g: (5A+3B=c+10D) as an input, i need to do a parsing for the string reaction so that i can extract &(split) integer values beside a char and put them into a vector i.e vector associated with the reaction here is :[5 3 1 10] i thought about std::strtok function but i think it cannot seperate integer values!!! can any one help me ?? here my try:

int main()
{
std::string input;
std::getline(std::cin, input);
std::stringstream stream(input);
while(1) {
 int n;
stream >> n;
char * pch;

pch = strtok (input," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}
return 0;

}

Forget strtok. It is useful for some stuff, but it DOES modify the input string. Write your own parser that walks through the string and extracts the numbers as needed. I do this sort of thing all the time - one of my jobs is to write parsers for customer data in order to provide "sane" data to our MES system. My latest one was to parse work-order CSV files and then push it to the database using a stored procedure for MS SQL Server. The parser runs on Linux. The database on Windows Server 2008.

The interesting wrinkle for that was to determine how to map the data elements to the fields of the stored procedure as each customer may use different mappings for their work order data!

I would "walk" through the line read from the file and use the IsDigit function to collect numbers and store them in a vector.

Try implementing it using regex.

@ddanbe - Along with the number in the vector, I would associate it with the variable that follows in his case, such a A, B, C, D, etc. Looks like the beginning of writing an infix parser for computing equations? :-) Been there. Done that!

@rubberman
I assume it's about chemical reactions, so operators and equal sign and the lot don't matter. At least this is what I guess.
So A and the rest are not variables in a math expression, but placeholders for chemical compounds. I happened to like writing a parser myself now and then. In fact I posted 3 snipets here about the matter(in C#)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.