int array[row][col];
Well first of all why are you using integers? Are you dealing with numbers or characters???
Also you are using with the array without initializing it, so it's garbage data.
1 2 3 4 5 6
for (j = 0; j < row; j++)
for (c = 0; c < col; c++)
for (i = 0; i < s.length(); i++)
{
cout << s[i] << array[row][col] << endl ;
}
in those lines you are not using the looping variables j and c when you access the array... cout << s[i] << array[row][col] << endl ;
so you are only accessing static positions that actually are outside the bounds of the array. You should look-up the documentation on usage of arrays.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string input;
cout << "Input the prefix expression." << endl;
cin >> input;
cout << "The expression tree is:" << endl;
string expr_tree[input.length()];
// This "*(new string(1, input[i]))" may be confusing.
// Basically, it makes the new string declared on the heap
// (see Wikipedia if you don't know what that is). Then it dereferences
// it because we're not dealing in pointers to strings
for (int i = 0; i < input.length(); i++) {
expr_tree[i] = *(new string(1, input[i]));
expr_tree[i] += " 0 0";
}
for (int i = 0; i < input.length(); i++) {
cout << expr_tree[i] << endl;
}
// we need to clean up because of that "new" back there
for (int i = 0; i < input.length(); i++) {
delete expr_tree[i];
}
return 0;
}
thanks for all the advice. I was able to get it to output the zeros however that was just so I could try and figure out the next problem. They are meaningful, if one of the operands (+, -, *, /) are entered then the second column needs to be the index of it's left child and the 3rd column its right so that the output will be
* 1 2
2 0 0
x 0 0
The only way I know to try and accomplish this is by a switch statement but I do not know how to try and find the index.