简体   繁体   中英

I'm trying to create a square for 2d array using the size of a string, but it's coming out rectangular

This program is supposed work by having the user input text, end the program using ctrl-z, and then the program puts the string into a square 2D array and then displays it from left to right top to bottom, and then top to bottom left to right. Unfortunately it becomes rectangular rather than a square.

Here's the code I have:

#include <iostream>
#include <string>
using namespace std ;

int main (int argc, char *argv[], char **env)
{
    string s ; s.clear() ;
    int c = cin.get() ;
    while (!cin.eof())
    {
        s += c ;
        c = cin.get() ;
    }

    for ( int i = 0 ; i < s.length() ; ++i)
    {
        if (!isalpha(s[i]))
        {
            s.erase(i,1) ; // removes non-alphanumeric characters
            --i ;
        }
    }

    int side = 1 ;
    while (side * side < s.length() )
    {
        ++side ;
    }

    cout << endl << side << endl;

    char block[side][side] ;
    int i = 0;
    for (int r=0; r<side; r++)    //This loops on the rows.
    {
        for(int c=0; c<side; c++) //This loops on the columns
        {
            block[r][c] = s[i];
            i++;
        }
    }

    cout << endl;

    for (int r=0; r<side; r++)    //This loops on the rows.
    {
        for(int c=0; c<side; c++) //This loops on the columns
        {
            cout << block[r][c];
        }
        cout << endl ;
    }

    cout << endl ;

    for (int c=0; c<side; c++)    //This loops on the columns.
    {
        for(int r=0; r<side; r++) //This loops on the rows.
        {
            cout << block[r][c];
        }
        cout << endl;

    }
    cout << endl;
    cout << s << endl ;
}

Sample input and output: This is a sample text for encryption

becomes: Thisisasampletextforencryption

Thisis
asampl
etextf
orencr
yption

Taeoy
hstrp
iaeet
smxni
iptco
slfrn

They're supposed to be 6x6. Instead they're 6x5 and 5x6.

In your example you are providing 30 characters of input but somehow expecting 36 characters of output (6x6). If you provide an additional character, you will end up with a partial last row/column in each of the displays. If you provide 36 characters of input, you will end up with a complete square for output.

You are reading past the end of the input, which is undefined behavior. You happen to be getting non-printable characters. You should do something like this:

for (int r=0; r<side; r++)    //This loops on the rows.
{
    for(int c=0; c<side; c++) //This loops on the columns
    {
        if (i < s.length())
        {
            block[r][c] = s[i];
        }
        else
        {
            block[r][c] = '*';  //or put anything else you want here
        }
        i++;
    }
}

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