I'm trying to make a really basic parser that will allow the user to enter in a string and will be able to search it for math problems. (ie 5+5)the main problem is right here. Also I'm still kinda sorta not really new to c++ so my terminology is lacking.
1 2 3 4 5 6 7 8 9
while (pos = input.find(delimiter_A))//lets say i enter 5+5
{
token = input.substr(0, pos);//token takes the 5 before the '+'
addition += stoi(token); //I thought this would convert the token and add the value multiple times
input.erase(0, pos); //this erases the string from the start to '+' so it can search for the next '+'
cout << "\n" << addition; //displays result or so i thought
}
Line 49 should be while ((pos = input.find(delimiter_A)) != string::npos) {
Line 54 should be input.erase(0, pos+1);
because you want to skip the + sign also.
Finally, when you exit the loop you'll have one last number to scan.
I tried using the program and I was able to get it working by adding this. I'm still a little confused about what npos does. Also I seem to be getting really dumb answers like 2+3 = 4
string::find() returns the position of the found string, or the constaint string::npos if it isn't found. The value of string::npos is an implementation detail but it certainly won't be zero since you can find the delimiter at position zero.
Also I seem to be getting really dumb answers like 2+3 = 4
Line 5 sets addition to the string value of the token, blowing away any previous value that it had. Line 7 then adds the same value to addition. As a result, the value of addition when you exit the loop will be the twice the value of the last token that you processed. And since you only process tokens when there is a '+' after them, the input "2+3" will result only process the token "2" and will set addition to 4.
Repeating myself:
Finally, when you exit the loop you'll have one last number to scan.
That makes more sense thank you. However I'm still unsure if I should use a while loop. I plan on having multiple delimiters. I'm going to experiment with other stuff. Thanks for the help