简体   繁体   中英

I'm struggeling with Multi-Dimension array input and output, I managed input, however printing doesn't work as planned

I'm playing around to get the grip of multi dimensional arrays. I have managed to have the array record the input from the user.. I'm trying to use 2 FOR loops to print with the idea that it should print 4 rows of 3 characters each

i know i can solve this if i manually type what to print, but for sure there is a way to have a loop do that for me...

here is the input and output code that i wrote:

    cout << "Enter characters" << endl;

    for (int i = 0; i < 4; i++)
    {
        for (int x = 0; x < 3; x++)
        {
            cin >> charArr[x][i];
        }
    }
    cout << "Printing the array now" << endl;
    for (int i = 0; i < 4; i++)
    {
        for (int x = 0; x < 3; x++)
        {
            cout << charArr[x][i];
        }
        cout << endl;
    }

i don't understand why some letters are gone and why it doesn't print in order...

where i is row and j is a column. for loop should come like this only. in this order only we can store the input. In your case, you are swapping the orders. Solution:

for (int i = 0; i < 4; i++)
{
    for (int j = 0; j < 3; j++)
    {
        cin >> charArr[i][j];
    }
}
cout << "Printing the array now" << endl;
for (int i = 0; i < 4; i++)
{
    for (int j = 0; j < 3; j++)
    {
        cout << charArr[i][j];
    }
    cout << endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM