For loop generating Run Time Error because of string condition

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..
So, I made the following program but the program is giving run time error due to inner for loop.

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
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int *resize(int *, int &);
int main()
{
	string a; 
	a = "5+10+100";
	int temp, b = 0, size = 0;
	int *num = new int[0];
	for (int i = 0, z = 0; i < a.length(); i++, z++)
	{
		for (; (a[i] != '+' || a[i] != '-'); i++)
		{
			b = b * 10;
			temp = a[i] - 48;
			b = b + temp;
		}
		num = resize(num, size); 
		num[z] = b; 
		b = 0; 
	}
	int sum = 0; 
	for (int i = 0; i < size; i++)
	{
		sum = sum + num[i];
	}
	cout << sum; 
	cout << endl;
	system("pause");
	return 0;
}
int *resize(int *num, int &size)
{
	int *temp = new int[size + 1];
	delete[] num;
	size++; 
	return temp; 
}


What I am trying to do here is this that as long as a "+" sign doesn't come, the loop will keep on adding the digits into variable "b". However, it doesn't seem to work.
Can anyone tell me what's the issue here, please? Thank you!
int *num = new int[0];
^^ this is not good. either make num null, or allocate some space (at least 1). c++ indexes from zero but you still have to have at least one block of memory...

also, the way size is changed looks wrong. its size zero, then zero again, then 1...

resize loses the data. is that ok?

you start with 5, and that is ok, but 100? it looks to go left to right, so it would turn 100 into 1 (1+ 0*10 + 0*100?). Unless I misread that loop, your string to numbers is busted.

are you allowed to use any tools here? I would personally take substrings off string a and convert them to numbers using stoi or something. And use a vector for num instead of this DIY memory stuff (unless you wanted to learn that?).
Last edited on
int *num = new int[0];
this actually worked for me previously so I used it again hehe. But yes, I get your point. From now on, will be allocating NULL to my pointer. thank you!

Change in size, I think it will do good. My code below will tell why exactly I changed it in that way.

Yes, resize will lose the data. I forgot to add one more line in it, thank you.

No, its actually a little different. This is what's happening here, (from the start)
0 * 10 = 0
0 + 1 = 1
1 * 10 = 10
10 + 0 = 10
10 * 10 = 100
100 + 0 = 100
So, that's how it is supposed to give me 100... I think it should work good.


I am allowed to use tools. Problem here is, I don't know much of tools. We are still in quite beginning of C++. We have studied Classes and Struct etc but haven't yet studied Vector so can't really use them. But yes, I can use substr and then use stoi to convert string into number. I didn't realize that.
Thanks for this suggestion... And following this, I tried to make a program, but again, it's giving a run time error :/

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, 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')
		{
			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
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?
Thanks a lot!
And also thank you, jonnin, for responding to my Question and giving some really valuable suggestions ^_^
Last edited on
Topic archived. No new replies allowed.