-1

In my piece of code, I have tried changing it from '' to '\0' as this is usually how you insert a blank character into an array but for some reason, it doesn't give me the expected output when trying this and I'm unsure why.

Output I am expecting is this:

#########
 #######
  ####
   ##

What I'm actually getting is this:

########




#include <iostream>
using namespace std;
int main (){
    char myArray[8] = {'#', '#', '#', '#', '#', '#', '#', '#'};
    cout << myArray << endl;
    int i = 0;
    while (i < 3){
     myArray[0+i] = '\0';
     myArray[7-i] = '\0';
     cout << myArray << endl;
     i++;
    }
    return 0;
}
hhh
  • 9
  • 4
  • 4
    `'\0'` is not a blank character. It denotes the end of the string. You meant to use `' '`(you can type this with the spacebar). – Ranoiaetep Nov 20 '22 at 05:49
  • For more about `'\0'`: [What does the symbol \0 mean in a string-literal?](https://stackoverflow.com/questions/4711449/what-does-the-symbol-0-mean-in-a-string-literal) – Ranoiaetep Nov 20 '22 at 05:51
  • 1
    Also your array isn't null-terminated at first (So `cout << myArray` on the next line leads to a buffer overrun). Use `char myArray[] = "########";` (Which will be an array of 9 characters, ending with `'\0'`) or `std::cout.write(myArray, 8)`. – Artyer Nov 20 '22 at 07:30

1 Answers1

1

Null character (\0) implies the end of a string in C++. Use a space character (' ') instead to achieve what you are trying to.

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39