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