-1

I'm trying to output the contents of an array, but whenever I run my code I get #/.

I don't understand what I'm doing wrong, and I don't have my textbook with me.

#include <iostream>

int main()
{
    int i, j;
    
    char arr[2][2] = { {15, 23}, {35, 47}};
    
    for(i=0; i<2; i++){
        for (j=0; j<2; j++){
            std::cout << arr[i][j];
            
        }
    }
}

There's no error message, just #/. I'm not quite sure what to try, either.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 5
    https://www.asciitable.com/ – tkausl Apr 12 '23 at 13:46
  • Which output do you actually expect? – πάντα ῥεῖ Apr 12 '23 at 13:47
  • Does this help: https://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout/19562163#19562163 ? – πάντα ῥεῖ Apr 12 '23 at 13:48
  • sorry for not putting this in the question. i was expecting to get 15 23 and then on a new line 35 47 – nobody65534 Apr 12 '23 at 13:49
  • 4
    Don't use `char` for integers. A `char` is for characters, really (and that's how it will be printed). So simple solution is to change the array base type from `char` to `int`. – Some programmer dude Apr 12 '23 at 13:49
  • Are you sure you are not just seeing your shell's prompt? – Jesper Juhl Apr 12 '23 at 14:01
  • what you get on the screen are the characters for the ASCII codes 15, 23 etc. If you want the compiler to deal with the >>numbers<< 15, 23 etc., declare the array as int, not as char. Currently the compiler sees your "char" declaration and cout also is informed, that these are characters and will print them as char. if you declare the array to hold "int", cout will see, that you have numbers and will convert e.g. the number "15" to the two characters "1" and "5" and print them ... that's the way the system "thinks". – Synopsis Apr 12 '23 at 14:08
  • 1
    `std::cout << +arr[i][j];` should provide the desired output. – Eljay Apr 12 '23 at 15:18
  • 1
    `char` is an integer type, just like `int`. But it's peculiar, because it's almost always used to store character codes, and all of the I/O routines assume that that's what it's doing. To display the actual values, use `std::cout << static_cast(arr[i][j]);` or, more succinctly, `std::cout << +arr[i][j];`. – Pete Becker Apr 12 '23 at 15:20

1 Answers1

0

Your array contains char elements, not int elements.

If you look as an ASCII table, like https://www.asciitable.com, you will see that ASCII code 35 is for character '#', ASCII code 47 is for character '/', and ASCII codes 15 and 23 are for non-printable characters.

So, printing 15/23 as char values won't display anything meaningful, and printing 35/47 as char values will display as #/.

To do what you are attempting, you need to print out the values as integers, not as characters, eg:

#include <iostream>

int main()
{
    int arr[2][2] = { {15, 23}, {35, 47} }; // <-- int, not char!
    
    for(int i = 0; i < 2; ++i){
        for (int j = 0; j < 2; ++j){
            std::cout << arr[i][j] << ' ';
        }
        std::cout << std::endl;
    }
}

On the other hand, if you actually need a char[] array, then you will have to convert the values when printing them, eg:

#include <iostream>

int main()
{
    char arr[2][2] = { {15, 23}, {35, 47} };
    
    for(int i = 0; i < 2; ++i){
        for (int j = 0; j < 2; ++j){
            std::cout << static_cast<int>(arr[i][j]) << ' ';
            // or:
            // std::cout << +arr[i][j] << ' ';
        }
        std::cout << std::endl;
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770