Hello!
I am having a problem with the following line of code:

int temporary = theMessage.size();
		int numberOfColumns;

		if (temporary%3 == 0)
			numberOfColumns = temporary/3;
		if (temporary%3 == 1)
			numberOfColumns = (temporary + 2)/3;
		if (temporary%3 == 2)
			numberOfColumns = (temporary + 1)/3;
		cout<<numberOfColumns<<endl;

int theNumbersOfTheMatrix[3][numberOfColumns];

theMessage is a string of modifiable length.
Until the point
cout<<numberOfColumns<<endl;
it works but then it says that the expression should have a constant value. Could somebody help me??

Dani AI

Generated

The compiler error happens because standard C++ requires built-in array sizes to be compile-time constants. Some compilers accept variable-length arrays as an extension, but that is non‑portable. See the C++ arrays notes for details: .

As noted, one quick hack is to pick a fixed upper bound. That works, but wastes space. suggested dynamic allocation, and implemented a pointer-per-row solution with new[]. That approach is valid, but it requires manual delete[] for each row and is error-prone. Prefer RAII containers in modern C++.

Two simple, safer patterns:

  • Vector of vectors (easy to read and resizeable)

    size_t cols = /* computed from message length */;
    std::vector<std::vector<int>> mat(3, std::vector<int>(cols));
    mat[0][0] = 1;
  • Single contiguous vector (better cache locality and easier to pass to numeric libraries)

    std::vector<int> flat(3 * cols);
    auto at = [&](size_t r, size_t c) -> int& { return flat[r * cols + c]; };
    at(2, 5) = 7;

If you want per-row dynamic arrays but with RAII, consider std::unique_ptr<int[]> for each row (no manual deletes). For matrix multiplication, ensure dimensions match (e.g., 3×N times N×M gives 3×M), use size_t for sizes, and prefer the contiguous layout for performance-sensitive code.

More on the containers: std::vector and std::unique_ptr.

Recommended Answers

All 7 Replies

numberOfColumns is not a "constant value" so you can't declare your array the way you did.

so how can i make a matrix of a fixed number of columns but with number of rows the length of a string?

Why not make the array large enough to hold the largest message you expect?

because its a matrix and i have to use it in a multiplication...

because its a matrix and i have to use it in a multiplication...

Yeah, so?

If you have a matrix defined int matrix[100][100] and you only load the values from matrix[0][0] thru matrix[19][29] , why would the rest of the matrix be used for multiplication? Just use the part that's loaded, thru matrix[20][30] .

Or you can use dynamic arrays.

i could not do this because its an encryption decryption thing... i figured it out though by using dynamic memory and defining the matrix as


int * MessageMatrix[3];
for (int i = 0; i < 3; i++)
MessageMatrix = new int [numberOfColumns];

Thanks anyway
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.