Filling a 2D Array from a Character Array

closed account (LEwpfSEw)
My assignment is trying to take an input from a user and store it into a character array and then count the number of characters so I can find the next greatest square number to implement as my dimensions for my 2D array.

However, I'm having trouble filling the 2D array, since I'm getting an error with saying " 'sqrsize' cannot appear in a constant-expression", and I don't know how to fix that.

I'm also not sure if I did line 56 correctly by incrementing the next "character" in the next column of that row until all the slots in that row are filled and it starts filling the next row.

I'm supposed to take the code in left to right/top to bottom, and print it out top to bottom/left to right, which is why I have the nested loops.


Parts of my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

void ReadMessage(char*, int);
void PrintMessage(const char*);
void RemoveNonLetters(char*, int);
int  FindLeastSquareRootNumber(int);
void FillArray (char*, int);

int main()
{
    int bufsize = 500;
    char buffer[500]; // Size of Character Array

    int sqrtnumber = 0;

    ReadMessage(buffer,bufsize);
    PrintMessage(buffer);

    RemoveNonLetters(buffer,bufsize);
    PrintMessage(buffer);

    sqrtnumber = FindLeastSquareRootNumber(strlen(buffer));

    cout << "sqrt num: " << sqrtnumber << endl;
}

int FindLeastSquareRootNumber(int number)
{
    int result = 0;

    for (;result < number; result++)
    {
        if (result*result > number) break;
    }

    return result;
}
void FillArray (char* input, int inputsize)
{
    int** CaeserBlock = 0;
    int sqrsize = FindLeastSquareRootNumber(inputsize);

    CaeserBlock = new int [sqrsize][sqrsize];




    for ( int r = 0; r < sqrsize; r++ ) // Loops the rows
    {
        for (int c = 0; c < sqrsize; c++) // Loops the columns
        {
              CaeserBlock[r][c]=input[c++];

        }

    }

    // Output of the Ciphered Array

    for ( int r = 0; r < sqrsize; r++ ) // Loops the rows
    {
        for (int c = 0; c < sqrsize; c++) // Loops the columns
        {
            cout << CaeserBlock[c][r];
        }
        cout << endl;
    }

}

Last edited on
You cannot do new int [X][Y]; They have to be defined individually.

e.g
1
2
3
int **array = new int[X];
for (unsigned i = 0; i < X; ++i)
 array[i] = new int[Y];


You need to use the same logic when deleting the memory to ensure you don't have a memory leak too.
Topic archived. No new replies allowed.