Last position of a string

I am making a program where a user writes a string and I have to "dissect" that string. I am running into a problem when I have to find the end of that string.
BTW, cStart and cEnd are integers.
1
2
3
4
5
    getline(cin,longString);
    cStart = longString.find("color=");
    cStart += 6;
    cEnd = longString.find("&",cStart);
    color = longString.substr(cStart, cEnd-cStart);


this will be in a web form and I just extract what is between the "=" and the "&" but I am stuck trying to search for a variable and the end of the string.
Let's say my last variable is "string color2"
I will do
1
2
3
4
cStart= longString.find("color2=");
cStart= 7 + (cEnd + 1);
cEnd = longString.find((string::npos),cStart);
color2 = longString.substr(cStart, cEnd-cStart);


but I end up getting stack over flow errors. How would I be able to find the last position of the long string? the part after what the user typed in.
1
2
3
4
5
6
7
8
9
getline(cin,longString);
size_t cStart = longString.find( "color=" );
if ( string::npos != cStart ) {
  cStart += strlen( "color=" );
  size_t cEnd = longString.find( '&', cStart );
  if ( string::npos != cEnd ) {
    // cStart and cEnd are valid and can be used
  }
}
I actually got around it by adding an "&" to the end of the string instead of looking for the end I will just keep looking for "&" symbols.

Thanks for the code I will also give it a try to make my code look cleaner.
Topic archived. No new replies allowed.