C++ Displaying a string (with blank spaces) into a two dimensional array?

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.


The problem is that the operator >> stops entering a string when a white space character is encountered in the stream. You should use standard function std::getline instead.

Take into account that to display a string there is no need to define an array. The task can be done without using an array.

Here is a demonstrative program.

#include <iostream>
#include <string>
#include <limits>
#include <algorithm>

int main() 
{
    while ( true )
    {
        std::cout << "Number of rows (0 - exit): ";

        unsigned int rows;
        if ( not ( std::cin >> rows ) or ( rows == 0 ) ) break;

        std::cout << "Number of columns (0 - exit): ";

        unsigned int cols;
        if ( not ( std::cin >> cols ) or ( cols == 0 ) ) break;

        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

        std::cout << "Type in text: (Enter - exit): ";

        std::string text;
        std::getline( std::cin, text );
        if ( text.empty() ) break;

        std::cout << std::endl;

        std::string::size_type n = text.size();

        n = std::min<std::string::size_type>( n, cols * rows );

        for ( std::string:: size_type i = 0; i < n; i++ )
        {
            std::cout << text[i];

            std::cout << ( ( i + 1 ) % cols == 0 ? '\n' : ' ' );
        }

        std::cout << std::endl;
    }

    return 0;
}

Its output might look like

Number of rows (0 - exit): 3
Number of columns (0 - exit): 4
Type in text: (Enter - exit): HarryPotter

H a r r
y P o t
t e r

Number of rows (0 - exit): 2
Number of columns (0 - exit): 6
Type in text: (Enter - exit): HarryPotter

H a r r y P
o t t e r 

Number of rows (0 - exit): 6
Number of columns (0 - exit): 2
Type in text: (Enter - exit): HarryPotter

H a
r r
y P
o t
t e
r 

Number of rows (0 - exit): 4
Number of columns (0 - exit): 4
Type in text: (Enter - exit): Antonella Masini 

A n t o
n e l l
a   M a
s i n i

Number of rows (0 - exit): 0

You may substitute this statement

if ( not ( std::cin >> rows ) or ( rows == 0 ) ) break;

for this statement

if ( !( std::cin >> rows ) || ( rows == 0 ) ) break;

if the compiler does not compile the first statement. If you use MS VC++ then in the properties of the project you should switch off using of the language extensions (C++, language), and the statement will be compiled.

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). The UNIX OS was totally written in C. Today C is the most widely used and popular System Programming Language. Most of the state-of-the-art software have been implemented using C.


cin will consider the space between Harry and Potter as a delimiter and only save Harry in text.

With cin.getline, the entire line is read until the Enter key is pressed. Therefore, with cin.getline(text, sizeof(text)), Harry Potter will be saved in text.

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.


you can use a dynamic array of characters so it is sized at run-time as the user input of rows and columns:

#include <iostream>
using namespace std;

int main()
{
    int row, col;

    cout << "Row: ";
    cin >> row;
    cout << "Col: ";
    cin >> col;

    char** cText = new char*[row];
    for(int i(0); i < row; i++)
        cText[i] = new char[col];

    cin.sync(); // clearing the input buffer
    cout << "Text: ";

    char czTmp[100];
    cin.getline(czTmp, row * col); // getting user input text which fits to our 2d array of chars

    int k = 0;
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            cText[i][j] = czTmp[k]; // inputting our array
            k++;
        }
    }

    cText[row - 1][col - 1] = '\0'; // adding a null-terminator because it is not added automatically

    // printing our array
    for(int i = 0; i < row; i++)
    {
        for(int j(0); j < col; j++)
            cout << cText[i][j];
        cout << endl;
    }

    // don't forget to free memory created by new

    for(int i(0); i < row; i++)
        delete[] cText[i];
    delete[] cText;
    cout << endl << endl;
    return 0;
} 

Why C is so Influential - Computerphile, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: Aug 18, 2015 C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available


How easy is it to learn C?, Why is C such an influential language? We asked ardent C fan Professor Brailsford. Brian Duration: 10:50 Posted: Aug 25, 2017 Discover historical prices for C stock on Yahoo Finance. View daily, weekly or monthly format back to when Citigroup, Inc. stock was issued.


How is C programming language still used today?, - and extremely flexible. So flexible, in fact, that as you use the language, you probably go through many different programming styles as you work out how to use the language to produce good, maintainable, reliable code. Programming Languages Development - C++ has been used extensively in developing new programming languages like C#, Java, JavaScript, Perl, UNIX’s C Shell, PHP and Python, and Verilog etc. Computation Programming - C++ is the best friends of scientists because of fast speed and computational efficiencies.


Which programs are written in C language?, is best choice when you start programming as it is most basic. Microsoft C++, C, and Assembler documentation. Learn how to use C++, C, and assembly language to develop applications, services, and tools for your platforms and devices.