'Table' doesnt format nicely when trying to print array in C++ -


so i'm trying print out table nicely padding so:

int rowlength = 8 * 4 + 8 + 1; char arr[rowlength];  fill(arr, arr + rowlength, '-'); string divider = arr;  for(int y = 0; y < 5; y++) {     cout << "\n" << divider << "\n";     for(int x = 0; x < 8; x++) {         cout << "|  " << "1";         if(x == 7)             cout << "|";     } } cout << "\n" << divider; 

and i'm expecting table print evenly, prints follows:

image 1

does know how can fix print code, evenly prints , without 2h @ end?

thanks!

string divider = arr; 

this std::string gets constructed arr, char array gets decayed char *. when constructing char *, char * must null-terminated string.

your code fills entire char array characters, fails append additional '\0' byte. results in undefined behavior.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -