Thread: Character won't print from an array

  1. #1
    Registered User
    Join Date
    Oct 2001

    Character won't print from an array

    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?

  2. #2
    Registered User
    Join Date
    Sep 2001

    3 things

    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Thanks for the reply, here is some additional information.

    1) The array was predefined and wasn't to be altered

    int floor[BoardSize][BoardSize] = {{0,0}},

    2) The call

    case PrintGrid:
    printArray (floor);


    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.

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    If it has to be an int array then you can cast -

    cout << (char)a[i][j];

  5. #5
    Registered User
    Join Date
    Aug 2001
    see my reply to your other thread. Changing floor to a char array rather than an int array seems to be more practical.

  6. #6
    Registered User
    Join Date
    Oct 2001
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I print out a two dimensional array of strings?
    By nellosmomishot in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 03:47 PM
  2. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  3. Passing a character array to a function
    By Qchem in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 07:18 AM
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM