Q:

Error when displaying the elements of an array

int path[10]={'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'};
for (int b=0;b<=9;b++) {
     cout << path[b];
}

When I try to run this to display a horizontal line it just shows a lot of 9s and 5s. But however if I run this one
for (int b=0;b<=9;b++) {
     cout << '_';
}

It seems to do the work. I know I can just use the 2nd bulk of code but I need to display it using the array.

A:

path is an array of int, when you print out its elements the int value (i.e. the ascii code 95 for '_') will be printed out. If you want it to be printed out as char, you should change the declaration from
int path[10]={'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'};

to
char path[10]={'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'};

