trying to parse a line

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

}



full code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//************************************
// C++ addition parser
//************************************
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

void analyze(string, int);
void help();

int main()
{
string input;
int size;
	for (;;)//infinite for loop
	{
		
		cout << "\n Enter formula or type '+_+' for help.\n";
		getline(cin, input);	//input string
		size = input.length();	//length of string

		if (input == "+_+")
			help();

		else
			analyze(input, size);

		
	}
system("pause");
return 0;
}


void analyze(string input, int size)
{
cout << input << "\n" << size;
string token;
size_t pos = 0;
	
char delimiter_A = '+';
int addition = 0;

	while (pos = input.find(delimiter_A))
	{
		
	token = input.substr(0, pos);
	addition += stoi(token);
	input.erase(0, pos);
	cout << "\n" << addition;

	}

}

void help() // help function
{
cout << "\naccepted operators\n" << "\nmultiplication = x, X or *\n" << "\ndivision = / \n" << "\naddition = + \n"
<< "\nexponent = ^" << endl;
cout << "\nwill accept parenthesis\n" << endl;
}

/*random note */
//isdigit()
//.at()
//size()
//getline(cin,str)
//void lexicon(char [], int );
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

1
2
3
4
5
6
7
8
9
10
	while ((pos = input.find(delimiter_A)) != string::npos)
	{
		
		token_1 = input.substr(0, pos);
		addition = stoi(token_1);
		input.erase(0, pos + 1);
		addition += stoi(token_1);

	}
		cout << "\n" << addition;


maybe a while loop isn't the best option here and I should try a different one.
Last edited on
I'm still a little confused about what npos does

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
Topic archived. No new replies allowed.