0

Beginner here having a problem with a dynamic 2D array of char that I don't quite understand. I need to fill the array with whitespace and then be able to select certain indices, which would contain whitespace characters, and replace them with something else. Would really appreciate some help.

I've managed to successfully fill the array in the way that I need, by looping through it and assigning ' ' to the current index.

However, when I use my other function, which is included below, something strange happens. The index with a whitespace becomes occupied by a 'W' (I see this by printing the array out) and the function seems to stop working as a result, because it's set to look for ' '.

If I fill the array with another character and set the function up to replace that character with a different character, it works fine.

 if (currentSpace == ' ' && movementChoice == 4)
    {
        pointerToBoard[antCurrentRow][antCurrentColumn] = '&';
    }

    else if (currentSpace == '&' && movementChoice == 3)
    {
        pointerToBoard[antCurrentRow][antCurrentColumn] = ' ';
    }

I've tried changing ' ' to " " in the function, but I end up with question marks.

I've tried storing ' ' in a char and using it in the function, but I end up with what I described above, a W shows up.

Even had a similar result if I use char(32) instead of ' ' in the function.

  • use `vector> pointerToBoard(/* boardRows = */ rows, vector(/* boardCols = */ cols, /* fillWith = */ ' ');` to fill with whitespaces – fas Oct 12 '19 at 11:04
  • @user3365922 Well, that's not the question, author said she/he managed to initialise correctly already (so let's assume it works correctly...). Additionally, she/he seems to be using raw arrays instead of vectors, but that's rather a guess from the name only. – Aconcagua Oct 12 '19 at 11:12
  • The code you provided is unlikely to produce other characters. Most likely the error lies elsewhere. However, without having concrete context, it is impossible to answer. You need to create a [mre] to enable us to help you. – Aconcagua Oct 12 '19 at 11:16
  • Looks pretty much as if it is time to learn how to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)... – Aconcagua Oct 12 '19 at 11:17
  • @Aconcagua Thank you for reading! I tried to cut down on what I was showing so people wouldn't have to read through that much, but here it all is: https://pastebin.com/291Krrmn I'm essentially saying that I'm not understanding why I can successfully fill it with whitespace and then look for the same character I used to fill it, but I end up with that weird 'W' behavior. It's as if my function recognizes it the first time, but changes it to a W, and then it won't recognize the same index again, because it was looking for whitespace, not a 'W'. – CodeForCookie Oct 12 '19 at 21:32
  • @user3365922 Thank you for your input, but I did manage to successfully fill it. My problem is I can't then go into the array and recognize it at certain indexes. – CodeForCookie Oct 12 '19 at 21:36
  • If the problem is solved, then please close or delete it. Thank you – Armin Montigny Oct 13 '19 at 08:09
  • At first, you shouldn't link to external resources, but include all code in the question, questions shall be self-contained. Then the code there is not complete, e. g. where do you get `currentSpace` from? How is it defined, when is is modified? Pretty suspicious: `else { pointerToBoard[antCurrentRow][antCurrentColumn] = currentSpace; }` (that's lacking in the question itself!) – I bet that currentSpace contains the `'W'` exactly at this point, but possibly you do a bad assignment yet elsewhere. – Aconcagua Oct 13 '19 at 08:43
  • By the way, you can do the filling much more efficiently: https://pastebin.com/guJmuCh6 – Aconcagua Oct 13 '19 at 09:05
  • @Acongua Wow, that's beautiful, thanks. I ended up figuring it out a day or so before you posted that. It's strange. I must have had a bug elsewhere that got resolved because it didn't seem I changed my method but it just started working. – CodeForCookie Oct 18 '19 at 01:32

0 Answers0