Problem in swapping of array indexes

Hello, I am making a program in which I am supposed to solve a mathematical expression using DMAS rule..
I am supposed to take an expression in a character array and then output the answer of it. I started working on it and have made up the following code for now (Please note that this program, for now, can only take numbers up to one digit and the following code is only up to division process... I have yet to add multiplication, addition, subtraction however the concept is same... Also note that I am ending the loop at the first division process just to test whether the program is doing its task correctly or not. In actual, loop won't end here)

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
#include <iostream>
using namespace std;
int main()
{
	char a[20], z = 0;
	for (int i = 0; i < 20; i++)
		a[i] = 'a';
	cout << "Enter Expression: ";
	cin >> a;
	bool fh = true;
	while (fh)
	{
		if (a[z] == '/')
		{
			int num;
			num = (static_cast<int>(a[z - 1]) / static_cast<int>(a[z + 1])) + 48;
			a[z - 1] = static_cast<char>(num);
			for (int i = z; i < 20; i++)
				a[z] = a[i+2];
			for (int i = 0; i < 20; i++)
				cout << a[i];
			fh = false;
		}
		z++;
	}
	system("pause");
	return 0;

}


So the idea here is to find the "/" in array, and then divide the number before it with the number next to it... And then after that, replace the current index with the current index+2.
(For Example, if expression is 5+2+3/1-4-1.
Then it will find "/", divide 3 with 1 and replace 3 with the answer, i.e. 1 and then replace "/" with "-", "1" with "4" and so on..
so, after first division, expression should be 5+2+1-4-1)
but my program isn't doing it, instead it is giving this result,
1
2
Enter Expression: 2+5/1+3-4
2+1╠1+3-4 aaaaaaaaaaPress any key to continue . . .


It is dividing 5 with 1 properly. But in swapping of indexes, instead of bringing "+3" back, it changes those two indexes instead to one garbage value and the next index with "1"
I don't get why it is doing this. and I can't seem to find out the error. Could anybody help in this regard?
Thank you!
Before you perform the operation you have to subtract 48 from each operand for getting the correct integer value.
num = (static_cast<int>(a[z - 1]-48) / static_cast<int>(a[z + 1]-48)) + 48;
I would have done this to shorten it a little and avoid that 48.
static_cast<int>( (a[z - 1]-'0') / (a[z + 1]-'0') + '0');
Oh, I was also making a mistake in the calculations. Thank you for the responses and thanks for correcting the mistake.
However, this still doesn't solve the other concern of mine :/ which is, moving the indexes two steps backwards in the array :/
I found still an error at line 19:
a[z] = a[i+2];
should rather be:
a[i] = a[i+2];

If you are interest, here is the code for a recursively working calculator, which can handle nesting by operator precedance and parenthesis.
http://www.stroustrup.com/dc_except.c

Here the source code will be explained, from page 242 onwards:
https://anekihou.se/programming/2.%20intermediete.pdf
Hello redfury,

The following code is not pretty, but it does show some points that can help you in the future.

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
#include <iostream>

using namespace std;  // <--- Best not to use.

int main()
{
        constexpr unsigned int MAXSIZE{ 20 };

	char numArray[MAXSIZE]{"5+2+3/1-4-1"};
	int pos = 0;
	int lenNumArray = strlen(numArray);  // <--- Added.

	//for (int i = 0; i < 20; i++)  // <--- Not needed if array is initialized when defined.
	//	a[i] = 'a';

	//cout << "Enter Expression: ";  // <--- Commented out for testing.
	//cin >> numArray;  // <--- Commented out for testing.

	bool fh = true;

	while (fh)  // <--- Could use "while (true) or while (1).
	{
		if (numArray[pos] == '/')
		{
			int num;

			int num1 = static_cast<int>(numArray[pos - 1]);
			int num2 = static_cast<int>(numArray[pos + 1]);
			//int num1 = static_cast<int>(numArray[pos - 1] - 48);
			//int num2 = static_cast<int>(numArray[pos + 1] - 48);

			// <--- Put a break point on the next line of code or put "cout" statements above this comment to see what num1 and num2 are doing.

			num = num1 / num2;

			numArray[pos - 1] = static_cast<char>(num + 48);

			for (int i = pos; i < lenNumArray; i++)
				numArray[i] = numArray[i + 2];

			std::cout << "\n numArray = " << numArray << std::endl;  // <--- If used do not need the for loop.

			lenNumArray = strlen(numArray);  // <--- Because length has changed.

			for (int i = 0; i < lenNumArray; i++)
				cout << numArray[i];

			fh = false;  // <--- Could change this line to break;.
		}

		pos++;
	}
	std::cout << "\n\n";
	system("pause");

	return 0;

}

Line 7. Defining this variable or something along the same lines means that if this need to change you only have to do it in one place. As a constant it means that the program can not change its value. This makes it useful when defining the size of an array. In for loops you can use "MAXSIZE" in the middle condition of a for loop and you will not have to hunt for the places that need changed.

Line 9. By initializing the array you accomplish two things. First is that you do not have to enter this information each time the program runs. This does temporally eliminate the need for lines 16 and 17, but since you know they are working that is OK. Second anything past the last character is set to "\0", which is what it should be not the letter 'a' that you use in the for loop.

When done testing just remove the quoted string and leave an empty set of {}s and this will initialize the entire array to "\0" again eliminating the need for the for loop.

I added line 11 because you need to know the length of the array that is used, so you only work with the part of the array that is used.

I added lines 27 - 30 to give you an idea of what is actually happening. Once you understand the you can put the code back into one line like you started with.

Lines 38 and 39. The original problem with the for loop is that you are trying to process more than you need to. You only need to work with the part of the array that is used.

On line 39 using the index of "z", now "pos", is not working. Since "z" never changes its value each time through the for loop what is on the rhs of "=" is being put in the same element of the array. By changing this to "i" the array element on the lhs of "=" changes each time through the for loop. You will see a big difference this way.

Line 41. Shows you that a "cout" statement can print the usable portion of the array and that the for loop is not needed. By initializing any unused part of the array to "\0" the last character moved in the previous for loop is "\0" marking the new end of the string.

I left the next lines of code to show you what is needed if you use a for loop. First you will need to get a new length of the string since it is shorter and the for loop needs to print out only the used portion of the array.

The comments in the program should explain the rest.

Hope that helps,

Andy
Topic archived. No new replies allowed.