separate string and numeric value from a string

Oct 21, 2016 at 1:38pm
hi forum,

The following snippet of code is not doing the task properly. I have large string with the following format:

 
 std::string("13 DUP 4 POP 5 DUP + DUP + -")


Separation is a whitespace and should be working with stringstream. But it is not. Please check the following snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	std::stringstream ss(std::stringstream::in | std::stringstream::out);
	ss << s;

	while (true)
	{
		unsigned long numericValue = 0;
		string operation = "";

		if (ss >> numericValue)
			list.push(numericValue);
		else if (ss >> operation)
		{
                     /*
                         further operation based on the string values
                     */
                }

                if(!ss)
                   break;
        }


The above code just extract the first value with the sample value and then cannot extract the string. Some hint is expected.

Thanks
Oct 21, 2016 at 1:56pm
The only time you will try to extract operation is when the stream fails to retrieve numericValue. When a stream fails it stays in failure mode until it is manually reset, and when a stream is in a fail state it will not process more information.

Perhaps something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
	std::stringstream ss(std::stringstream::in | std::stringstream::out);
	ss << s;

	unsigned long numericValue = 0;
	string operation = "";
	while (ss >> numericValue >> operation)
	{
		list.push(numericValue);
             /*
                         further operation based on the string values
             */
            
        }



Oct 22, 2016 at 6:47pm
Hi ,

But what about the format "+ -", where you have no numeric value in the input at all ?


Thanks
Topic archived. No new replies allowed.