Could someone explain to me, why there are 4 additional slots in char tab[], when I asked only for 3? How to get rid of them? I'm coding in Visual Studio 2017. Edit: the first program was very basic and didn't show what I intended. So, there is an extended one.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int i, n;
vector<char> input;
char chp;
cout << "Enter a expression" << endl;
while (1)
{
cin.get(chp);
if (chp == '\n') break;
input.push_back(chp);
}
n = input.size();
char* tab = new char[n] {};
for (i = 0; i < n; i++)
{
tab[i] = input[i];
}
int l = strlen(tab);
for (int i = 0; i < l; i++)
{
cout << "tab[" << i << "] is " << tab[i] << endl;
}
cin.get();
}
Result in console window is similar, when I enter "3+3"
tab[0] is 3
tab[1] is +
tab[2] is 3
tab[3] is ř
tab[4] is ř
tab[5] is ř
tab[6] is ř
This isn't still the full program (full program is a calculator, that calculates any math expression, and is much longer). I wrote that in C long time ago, and in C dynamic arrays are not such a problem.
Also, what about multidimensional arrays? Can string be a solution also for them?