Er: c2106 in VsC++ Express

closed account (yCf9216C)
I'm determined to finish this program on my own, and maybe I just need a break, but no matter what I do I get this error.

Here's my 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
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

#include<string>
#include<iostream>
#include<sstream>
using namespace std;
int isnumber(string mystring, int loc) {
	const int NoConstSt = 49;
	const int NoConstEn = 58;
	cout << "The operation is: " << (int) mystring.at(loc) << "\n";
	if ((int)mystring.at(loc) >= NoConstSt && (int)mystring.at(loc) <= NoConstEn)  return 1; //This returns one, which to the program will mean "this container holds a number".
	else if  ((int)mystring.at(loc) == 45) return 2; //Means we do subtraction.
	else if ((int)mystring.at(loc) == 43) return 3; //Means we do addition.
	else if ((int)mystring.at(loc) == 42) return 4; //Means we do multiplication.
	else if ((int)mystring.at(loc) == 47) return 5; //Means we do division.
	else if ((int)mystring.at(loc) == 40) return 6; //Means we have an opening parantheses.
	else if ((int)mystring.at(loc) == 41) return 7; //Means we have a closing parantheses.
	else if ((int)mystring.at(loc) == 46) return 8; //Means we have a decimal.
		
	else return 0;
}

double compute ( double var1, double var2, int operand) {
	switch ( operand ) 
				{
				case 2:
					return var1 - var2;
				case 3:
					return var1 + var2;
				case 4:
					return var1 * var2;
				case 5:
					return var1 / var2;

				default: 
					return 0;
					
	}

}

double extractnumber (int startloc, string &mathexpression) {

	int b;
	for (int unsigned c = startloc; c <= mathexpression.length() - 1; c++ ) {
		static string mystring;
		static int iteration = 0; //Counts the number of iterations.
		double result;
		iteration++;
		b = isnumber(mathexpression,c);

		if (b >= 2 && b <= 7 || c == mathexpression.length() - 1) {
			cout << "Mathexpression before number deletion: " << mathexpression << " Iteration is: " << iteration << "\n";
			stringstream (mystring) >> result;
			mathexpression.erase(startloc, c + 1);
			
			cout << "Mathexpression after number deletion: " << mathexpression<< "\n";
		return result;
		}
		else if (b == 8) {
			continue;
		}
		else if (b == 1){
		mystring = mystring + mathexpression.substr(c);
		}
	
	}


}

int main () 
{

	double var1 = 0;
	double var2 = 0;
	double var3 = 0;
	int initvar = 0;
	int closevar = 0;
	int endvar;
	bool parantbool = false;
	bool usebool = false;
	int operand = 0;
	int operation = 0;
	double result = 0;
	string OO;
	string mystr;
	cout << "Enter a series of mathematical operations: \n";
	getline (cin, OO); //Enter "5+5("
	endvar = OO.length() - 1;
	for (int signed c = 0; c <= endvar;  c++ ) {
	
		operand = isnumber(OO, c);
		endvar = OO.length() -1; 

		if (operand == 6 && parantbool == false) {
			initvar = c;
			cout << "initvar is: " << initvar << " \n";
		}
		 else if (operand == 7 && parantbool == false) {
			closevar = c;
			cout << "closevar is: " << closevar << "\n";
			parantbool = true;
		 }
		 else if (c == endvar && parantbool == true) {
			 c = initvar;
			 endvar = closevar;
		 }
		 else if (operand == 1 && parantbool = true) {
			 if (usebool == false) {
				 var1 = extractnumber(c + 1, OO);
			 }
			 else if (usebool == true) {
				 var2 = extractnumber(c, OO);
				 result = compute(var1,var2,operation);
				 cout << "Result is: " << result;

			 }
		 else if (operand >= 2 && operand <= 5) {
			 operation = operand;
		 }

		 }
	}
	cin >> OO;
	return 0;
}
//Just for a note: / = 47, * = 42, + = 43, - = 45, ( = 40, ) = 40, and . = 46 . These are the operators (in ASCII) 


The bolded code is where I'm getting the C2106 error. That error means you have an invalid assignment, like 1 = a; instead of a = 1; , which makes no sense!

What it does is perform order of operations. The function Isnumber returns a number which can be checked against a value to see if the string container contains the type of ASCII character you want. I could just use an enumeration, but I've never used one before, and at this point I'm not going to.

It's not finished!
parantbool = true should be parantbool == true
else if (operand == 1 && parantbool = true) {
I think that should be parantbool == true. It shouldn't give you an error the way it is though.
closed account (yCf9216C)
Ah, judging by the error and that it was at build time the error was generated, I knew it must be the syntax. I kept looking at that line missing what was wrong! Thanks!
closed account (yCf9216C)
I'm having some more trouble, I just can't nail down why I'm getting an out of bounds exception every time I reach the string.erase function, in my extractnumber function. I've looked at the documentation for this function on this site, and I still don't know why I'm getting this error.

The function deletes the numbers taken out of the mathexpression string, because that makes it easy to skip them while storing their result in a variable. There's no need to keep them, because they have already been calculated.

The function as it stands:
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

double extractnumber (int startloc, string &mathexpression) {
	int b;
	double result = 0;
	int  length = mathexpression.length() - 1;
	for (int  c = startloc; c <= length; c++ ) { 
		static string mystring; //To store the accumulated containers up until the first operator is found.
		static int iteration = 0; //is checked against startloc + the length of the string, if greater than that, we only delete up until the end.
		length = mathexpression.length() - 1;
		b = isnumber(mathexpression,c);

		if (b >= 2 && b <= 7 || c == length) { //Checks for operators. Parantheses are included (6&7).
			cout << "Mathexpression before number deletion: " << mathexpression << " Iteration is: " << iteration << " mystring is: " << mystring << "\n";
			stringstream (mystring) >> result; //Puts the accumulated charcters into an integer, to create a valid integer.
			if (startloc + iteration <= length -1 ) { //Checks against length - 1 to check if the erase function will go beyond the strings length.
			mathexpression.erase(startloc, iteration);
			}
			else if (startloc + iteration >= length ) 
		{	mathexpression.erase(startloc); } //Delete the rest if we only have one left, or whatever.

			iteration = 0; 
			mystring.clear();
			cout << "Mathexpression after number deletion: " << mathexpression<< "\n"; 
		return result;
		}
		else if (b == 8) { //we have a decimal
			continue; //skip this place in the string.
		}
		else if (b == 1){ //Number exists in container.
			iteration++;
			mystring = mystring + mathexpression.substr(c,1); //Accumulate/append current container to mystring.
		}
		else {cout << "Else found in extractnumber at: " << iteration << "\n";}

	}

}
When you are erasing the string its length is decreased. However the main loop uses the original length of the string.
Topic archived. No new replies allowed.