hi, i have this code:

#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main (char argc)
{
    string n1 = "141+246+3+64";
    int n2;
   	int pos = -1;	
    char toFind = '+';	
    do
    {		
         pos = n1.find(toFind, pos+1);	
         if(pos != -1)
         {
                stringstream(n1) >> n2;
                cout << n2 << endl;
                cout << n1 << endl;
                cout << pos << endl << endl;
                n1 = n1.substr(pos);
         }	
    } while( pos != -1 );
    system("pause");
}

this is the output: 141
141+246+3+64
3

246
246+3+64
5

Press any key to continue . . .

I dont know why it wont find all of the +'s. Any solutions?

Recommended Answers

All 2 Replies

line 13: remove pos+1

line 20: add pos+1 in the substr

The two lines mentioned above are conflicting with each other.

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.