Explanation as to how this code works? Using a string for mathematical equation

Hello, I am trying to write a program in which I will be using a string to add or subtract a mathematical equation. That is, a mathematical equation will be inputted in a string and then that will be used to produce an end result..

I tried to make a program, but it is giving run time error and not working properly. According to my logic, it should be working. Here's the code.

(Please note that this is simply a code to separate integers from string.. Once it is done, then mathematical operation is very easy)

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
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
	string a = "5+10+100"; 
	int f = 0, size = 0;
	int *num = NULL, *temp; 
	string t; 
	for (int i = 0; i < a.length(); i++)
	{
		if (a[i] == '+' || a[i] == '-' || a[i+1] == '\0')
		{
			if (a[i+1] == '\0')
				t = a.substr(f, i);
			else 
				t = a.substr(f, i-1);
			f = i + 1;
			temp = new int[size + 1];
			for (int i = 0; i < size; i++)
				temp[i] = num[i];
			delete[]num;
			num = temp; 
			num[size] = stoi(t);
			size++; 
		}
	}
	for (int i = 0; i < size; i++)
		cout << num[i] << endl; 
	cout << endl;
	system("pause");
	return 0;
}

The logic here is this that there is first index number which will be stored in variable int... And the last index number will be whatever stored in "i".
So, whenever "+" comes, I will take a substring from "f" to "i-1" (the character before "+"), i.e. "5" in first case.
However, there is still an error being generated on num[size] = stoi(t) and I have no idea why is it so :/
(You can see how I am using size here.. Basically it stays zero so that I can add a value into that index i.e. 0th index for first case. And then I make an addition in it.)




Now, I changed a little bit of code..
And declared another int "l" for last number of substring.. I forgot to change its value, but it still worked well.

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
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
	string a = "5+10+100"; 
	int f = 0, l = 0, size = 0;
	int *num = NULL, *temp; 
	string t; 
	for (int i = 0; i < a.length(); i++)
	{
		if (a[i] == '+' || a[i] == '-' || a[i+1] == '\0')
		{
			t = a.substr(f, l-1);
			f = i + 1;
			temp = new int[size + 1];
			for (int i = 0; i < size; i++)
				temp[i] = num[i];
			delete[]num;
			num = temp; 
			num[size] = stoi(t);
			size++; 
		}
	}
	for (int i = 0; i < size; i++)
		cout << num[i] << endl; 
	cout << endl;
	system("pause");
	return 0;
}


It is giving a perfect result.
1
2
3
4
5
6
5
10
100

Press any key to continue . . .



But I don't get the reason as to why?

First thing, the value of "l" never changed from '0' so how come it is still getting 10 or 100? Because in both cases, the last value isn't 0 but instead it is '3' in case 10 and '7' in case 100. So how is substring working perfectly in this case?

And now, even if we consider that the index stored in int l is correct then how is it working for 100? Because the loop works until the last index of the string (which is '7' in case of this string)
Then it checks whether a[i+1] == '\0' is true... Which will come out to be true.
And then it makes a substring of the main string by,
t = a.substring (f, l-1)
so, in this case, values will be,
t = a.substring (5, 7-1)
That means, it should only take "10" into consideration and store "10" into the string 't'
But it is giving 100 in the output, meaning it stores "100" into the string 't'.
Can anyone explain that why and how is this working?
Thank you.
first, you need to be clubbed with a wet noodle for about 20 min for using l and 1 together and for variable names in general. Goodness.

that aside, lets take a look.
http://www.cplusplus.com/reference/string/string/substr/ tells us that if the length parameter is too big, it uses what it can, effectively size(). Length is unsigned (its a size_t which is unsigned int of some flavor).
so, l-1 is 0-1 which is -1. -1 happens to be the largest possible bit value of the result, that is, if its a char this is 0xFF. the reason is 2s complement: flip the bits and add 1 for negative numbers. -1 then is binary 1 (000001) flip the bits (111110) and add 1 (111111). Now take 111111 as an unsigned value (because you stuck it into size-t) and its the biggest value it can be. So length is bigger than the string length, see documentation on substr... see how it fits together?

strings ignore \0. they use size() instead. you are trying to do something c-stringish here that isnt doing what you think it is doing. Don't try to mix C string concepts with c++ strings until you are more expert in both systems (and at that point, mixing them is usually not productive with one or two exceptions). if it were a c-string, the zero would be in the [8] position.

its confusing at first but length and zero indexing play off each other as well.
the length of your string is 8. the index is 0-7. so .length() returns 8 which gives you 0-7 when you loop on < length...
1
2
5+10+100
01234567


its looping 0-7, the 7th location is the second zero of 100. Its right.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
   string a = "5+10+100";
   int result = 0;
   stringstream ss( a );
   for ( int i; ss >> i; ) result += i;
   cout << a << " = " << result << '\n';
}

5+10+100 = 115
Topic archived. No new replies allowed.