I know that there are already several questions answered about this topic but none of them could help me solve my problem. Please, have in mind that I have just started learning C++ Programming.
I am creating a program that reads a text and then displays it in N rows x M columns (entered by the user). So, if the user writes HarryPotter and wants it to be displayed in a 3 x 4 array it should look something like this:
H a r r
y P o t
t e r
I have already manage to do so with this code:
cout << "Number of rows: ";
cin >> nr;
cout << "Number of columns: ";
cin >> nc;
cout << "Type in text: ";
cin >> text;
char letters[100][100];
for (int row = 0; row < nr; row++)
{
for (int col = 0; col < nc; col++)
{
letters[row][col]= text [i];
i++;
}
}
cout << "Print array: " << endl;
for (int row = 0; row < nr; row++)
{
for (int col = 0; col < nc; col++)
{
cout << letters[row][col];
}
cout << "\n";
}
And it works fine until the user writes more than one word. For example instead of HarryPotter he writes Harry Potter (I think that the blank spaces between the words is what creates the problem). Do you know why this happens and how can I solve it? Thank you very much.