0
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.

2
  • 1
    Distinguish char and int. Commented Jul 19, 2016 at 10:12
  • Oh my. Such a terrible mistake. Thank you! Commented Jul 19, 2016 at 10:13

2 Answers 2

3

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]={'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'};
Sign up to request clarification or add additional context in comments.

Comments

1

In the first example you declare path as an array of int and it gets interpreted by std::cout like an int (ASCII code for _ is 95). To make it work, you need to change the declaration to:

int path[10]={'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'};

In the second example you output '_' to std::cout, which is a char type and is displayed as you expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.