find_first_not_of problem

I've been working on a parser for a while and i seem to be having an error with a certain bit of code "find first not of". The main issue i'm having is that whenever I enter a number bigger than 99 the program only reads the first two numbers.

*code is not complete yet
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
string.legnth; //assume it is 5+500
string token_1;    //declaration of token 1 & 2
string token_2;
size_t pos_1 = 0;   //pos_1 necessary to save point on a string

int addition = 0;
double answer = 0;
for (int i = 0; i < input.length(); ++i)
{
	if(isdigit(input[i]) && answer == 0)
	{
		pos_1 = input.find_first_not_of("1234567890");
		token_1 = input.substr(0, pos_1+1);
		cout << "token_1=" << token_1 << endl;
		answer = stoi(token_1);
		token_1 = "";
	}
	else if (isdigit(input[i]) && answer != 0)
	{
		continue;
	}
	else if (input[i] == '+')
	{
		input[i] = 'A';
		pos_1 = input.find_first_not_of("1234567890");
		token_2 = input.substr(i+1, pos_1+1);
		cout << "token_2=" << token_2 << endl;
		answer += stoi(token_2);


What's weird is that when I enter 500+5 it will work for the first number but not for the second number/token. Does anyone have any ideas? Also if you have any questions please ask.











IMPORTANT!!: you don't have to look at the entire code but here it is if you're truly interested.
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//************************************
// C++ addition parser
//************************************
#include <iostream>	//headers needed and ones i put in cause 
#include <cstdlib>	//im paranoid
#include <stdlib.h>
#include <string>
#include <sstream>



using namespace std;

int addition(int[]);	//in progress
void analyze(string);	//analyzingsection
void help();	//help section

int main()
{
	string input;	//string that user enters
	for (;;)//infinite for loop
	{
		cout << "\n Enter formula or type '+_+' for help.\n"; // prompt
		getline(cin, input);	//input string
		

		if (input == "+_+")
			help();	//acces help function

		else
			
		analyze(input);	//access analyzer
	}
system("pause");
return 0;
}

void analyze(string input) //analyzer function
{


string token_1;
string token_2;
size_t pos_1 = 0;
size_t pos_2 = 0;
size_t pos_3 = 0;

int addition = 0;
int subtraction = 0;
int multiplication_product = 1;
double answer = 0;

for (int i = 0; i < input.length(); ++i)
{
	if(isdigit(input[i]) && answer == 0)
	{
		pos_1 = input.find_first_not_of("1234567890");
		token_1 = input.substr(0, pos_1+1);
		cout << "token_1=" << token_1 << endl;
		answer = stoi(token_1);
		token_1 = "";
	}
	else if (isdigit(input[i]) && answer != 0)
	{
		continue;
	}
	else if (input[i] == '+')
	{
		input[i] = 'A';
		pos_1 = input.find_first_not_of("1234567890");
		token_2 = input.substr(i+1, pos_1+1);
		cout << "token_2=" << token_2 << endl;
		answer += stoi(token_2);

	}
	else if (input[i] == 'x')
	{
		input[i] = 'M';
		pos_1 = input.find_first_not_of("1234567890");
		token_2 = input.substr(i + 1, pos_1 + 1);
		cout << "token_2=" << token_2 << endl;
		answer *= stoi(token_2);


	}

	else

		break;
	
}
cout << "\n" << input << "=\t" << answer;
}



void help() // help function
{
	cout << "\naccepted operators\n" << "\nmultiplication = lower case 'x'\n" << "\ndivision = '/' \n" << "\naddition = '+' \n"
		<< "\nexponent = ^" << endl;
	cout << "\nparenthesis in progress\n" << endl;
}


/* NOTES FOR DAYS

//isdigit()
//.at()
//size()
//getline(cin,str)
//void lexicon(char [], int );
//input.find_first_of('('
//isdigit[]
//input.find_first_not_of("1234567890");
/*switch (input.at(i))
{
case isdigit:
pos_1 = input.find('+');
token_1 = input.substr(0, pos_1);
input.erase(0, pos_1 + 1);
cout << token_1;
break;
default:

break;
}


for (int i = 0; i < number_counter; i++)	//where the math happens..this will be moved into an addtion function
{
pos_1 = input.find('+');
pos_2 = input.find(0-9);
token_1 = input.substr(0, pos_1);
input.erase(0, pos_1 + 1);
addition += stoi(token_1);
}
cout << addition;

*/
Please show a sample of your input string.

Also be careful with the string.find functions. These functions return a value between zero and std::string::npos with zero being the first character of the string and std::string::npos is std::numeric_limits<std::string::size_type>::max(), the value that represents the largest possible std::string.length(). This usually indicates that the find() was unsuccessful.

So in the following snippet if the first character is not a digit pos_1 will equal zero.
1
2
		pos_1 = input.find_first_not_of("1234567890");
		token_1 = input.substr(0, pos_1+1);


If however there are only digits in the string pos_1 will be equal to std::string::npos and in this case trying to add one to this value will also yield a value of zero.

I'm sorry I'm not great with programming terminology. I entered 5+500 for the input. I found that I can get the first token just fine, but the real issue is the second token.

i did find a solution for the problem although I'm not so sure how it works, but here it is
1
2
3
4
5
6
input[i] = 'A';
		pos_1 = input.find_first_not_of("1234567890");
		token_2 = input.substr(i+1, pos_1+input.length());
		cout << "token_2=" << token_2 << endl;
		answer += stoi(token_2);

I added the length of the string to pos_1, but shouldn't that = the current position of pos_1 + the entire length of the string? I've also added values like 1000 and have seen similar results.

Update: even if i add pos_1 - input.length() it still works. Heck I'm sure i could put pos_1 - unicorn and it would still work...Am I drunk
Last edited on
Have you considered using a stringstream to parse the line? Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void analyze(string input) //analyzer function
{
   stringstream ins(input_string);
   int first_number, second_number;
   char operator;
   int answer;

   ins >> first_number >> operator >> second_number;

   switch(operator)
   {
      case '+':
          // DO your addition operations.
          answer = first_number + second_number;
      case '-':
          // Do your subtraction operations.
...


what's a string stream? Also for this parser I want it to be able to read everything the user enters not just 2 values. It does seem like a good basic code though I could probably use it for a function. thanks
Hi,

I thought this might help:

https://en.wikipedia.org/wiki/Shunting-yard_algorithm



what's a string stream?


Google std::stringstream
Topic archived. No new replies allowed.