I have a 2 dim array a [20] [20} with the character * in some locations. It was loaded as Character = '*' into the location. When I cout a[x][y], I get the ASCII number. In this case 42 instead of the *. Am I loading it wrong or printing it wrong?
I have a 2 dim array a [20] [20} with the character * in some locations. It was loaded as Character = '*' into the location. When I cout a[x][y], I get the ASCII number. In this case 42 instead of the *. Am I loading it wrong or printing it wrong?
3 things, first of all, if you didn't in your code, you're missing an inserion operator in your code. It should be cout << variable;.
Second, I had a similar problem and solved it by passing a pointer. But that may not be the problem (I just had similar symptoms), so also check that you didn't declare the arrays as type int, because they're automatically converted and would not have cause a problem until you tried to print them.
Thanks for the reply, here is some additional information.
1) The array was predefined and wasn't to be altered
declaration
int floor[BoardSize][BoardSize] = {{0,0}},
2) The call
case PrintGrid:
printArray (floor);
break;
3) a [ row ][column] = DrawChar;
DrawChar is a global variable
const char DrawChar = '*';
const char BlankChar = ' ';
4) here is the output function
void printArray(int a[][BoardSize]){
int i,j;
for (i =0; i < BoardSize; i++){
for (j = 0 ; j < BoardSize; j++)
cout << a[i][j];
cout << "\n";}
}
It appears that it violates your no int definition.
Is there any other way? I can't make the array a string array. But there should be a way to convert the ascii number to the character.
If it has to be an int array then you can cast -
cout << (char)a[i][j];
zen
see my reply to your other thread. Changing floor to a char array rather than an int array seems to be more practical.
The cout << (char) a[i][j]; didn't work. I ended up with a series if if else statement. This worked but not pretty. I'm glad I only had two char.
BTW - cout << (char) a[i][j]; statement did not work in MSVC++ or the version in the Deitel book. It caused an error with or with the ( ).
Thanks to all for the help. This forum really helps because it makes me write out the question which makes me walk through the real problem.