-4

I need help to what seems to be a simple task, but I cannot find how to do this.

I have a form and a button. In the 'button_click' event I want to declare an array like - String myArray [20,50]-

Then I want to populate the array withlines of text that I am reading from a ,txt file. That is, 50 line X 20 character. (I know how to read the file)

Could anyone show me the correct syntax/method to get this working.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • 3
    C++ is usually not the best language to figure out as you go; I would recommend looking for books or tutorials online. – Daniel H Jul 19 '17 at 19:58
  • 1
    You probably want something like `std::array50> myArray;` – user0042 Jul 19 '17 at 19:59
  • Go run through some tutorials. – DMrFrost Jul 19 '17 at 19:59
  • 2
    Possible duplicate of [initializing a 2 dimensional array of strings](https://stackoverflow.com/questions/4612328/initializing-a-2-dimensional-array-of-strings) – Dalton Cézane Jul 19 '17 at 19:59
  • A string already contains multiple characters; a 2D array of strings would have three dimensions for characters. Additionally, C++ doesn’t have 2-dimensional arrays *per se*; it has arrays of arrays. I would recommend either `std::array, 50>` or `char[20][50]`. – Daniel H Jul 19 '17 at 20:01
  • @FrancescoC read [ask]. – dandan78 Jul 19 '17 at 20:05
  • 1
    Prefer `std::array` or `std::vector` over C-style arrays in most (almost all) cases. – Jesper Juhl Jul 19 '17 at 20:11
  • Hi, the syntax of your suggestions gives a lot of errors. I think that is plain C++. I need code for VIsual C++ following - private void closeBtn_Click(object sender, EventArgs e)- Then I would declare my array . – FrancescoC Jul 20 '17 at 08:16

1 Answers1

0

There are many way to declare a two dimentional [sic] array of strings:

Strings

std::string matrix[MAX_ROWS][MAX_COLUMNS];
std::vector<std::vector<std::string> > matrix_vectors;
std::array< std::array<std::string> >  matrix_array;
std::string matrix[MAX_ROWS * MAX_COLUMNS];

C-Style strings
A C-Style string is an array of char terminated by a nul, '\0', character.

Since it is an array, we'll be create a 3 dimensional array of characters:

char matrix[MAX_ROWS][MAX_COLUMNS][MAX_STRING_LENGTH];
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154