Bah, once again I am beat by the computer logic...
Anyone have an answer as to why it gives me an error: "Expected a constant expression at msg"

Here's the code,

``````const int INFOWND_SIZE = 4;
int msg_length[] = "Message";

double calc_amount = (double)msg_length/(double)INFOWND_SIZE;

int msg_amount = (int)ceil(calc_amount);

char msg[msg_amount][INFOWND_SIZE];``````

Anything unclear? eh.. let's take a look...

• declaring a constant int with the value 4
• divide length with size, giving a double
• declare variable int with a cast (int) to make sure it calculates int, and then do the ceil()
• declaring a char array with 2 dimensions, first one is the amount calculated, second is the size

Please try and give a simple answer that I can understand. :P
Constructive criticism, suggestions, anything that might optimize the code further is much appreciated, as long as you explain why.

//----------------------------------------------
Here's the whole code in context

``````const int INFOWND_SIZE = 51;

void new_message(char var[]){
COORD coord = {(infoffset.X+1), (infoffset.Y+1)};
int real_infownd_size = INFOWND_SIZE-4;
int msg_length = strlen(var);
if( msg_length >real_infownd_size ){
double calc_amount = (double)msg_length/(double)real_infownd_size;
int msg_amount = (int)ceil(calc_amount);
char msg[msg_amount][INFOWND_SIZE];

cout<<endl<<"length: "<<msg_length<<endl;
cout<<"MAXIMUM: "<<INFOWND_SIZE-4<<endl;
cout<<"amount of divs: "<<msg_amount<<endl;
}
}``````

This is a simple program I'm making, a sort of chat window, but not too complex yet. Anyway, at the start, I call the function with a parameter of 107. Rounded up it should be 3, the cout statement agreed, and it doesn't display a double value. Once I declare the variable named "msg_amount" as a const, it just piles up more errors...

The whole problem involves the section where I declare char msg[msg_amount][INFOWND_SIZE], where the compiler tells me it expected a const value. The whole idea of this is to take the string and divide it in sections so that the text won't overwrite other outputed stuff in the info window (infownd) since the window itself has a size.

Does this code compile for you?

``````int main(){
size_t cols = 10;
cin >> cols;
size_t rows = 20;
cin >> rows;

char x[cols][rows];
for(int i = 0; i < rows; i++){
for(int n = 0; n < cols; n++){
cout << x[i][n];
}
cout << endl; …``````

... size_t is a standard type (~100% of the time it's an unsigned int)... that's weird. Could you copy and paste what your compiler is saying please? :)

What archaic compiler are you using? Hehe. Get GCC, it supports (practically) all the C++ features + a bit more.

I know that's the problem, and MSVC++ is sometimes quite archaic when it comes to "C code". You can try and use the vector class? MSVC++ apparently can't create 2D "standard" arrays dynamically.

You can of course create the array yourself with new, see:

All 14 Replies

Does this code compile for you?

``````int main(){
size_t cols = 10;
cin >> cols;
size_t rows = 20;
cin >> rows;

char x[cols][rows];
for(int i = 0; i < rows; i++){
for(int n = 0; n < cols; n++){
cout << x[i][n];
}
cout << endl;
}

return 0;
}``````

Ah, no it doesn't. I don't think it likes it when you say size_t, what is that anyway? I've seen it before.

The problem is about why the compiler doesn't accept the integer when used in declaration of char array.

here's a functional code snippet

``````#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <conio.h>
#include <windows.h>
#include <string.h>
#include <cmath>

using namespace std;

const int INFOWND_SIZE = 51;

void new_message(char var[]){
int real_infownd_size = INFOWND_SIZE-4;
int msg_length = strlen(var);
if( msg_length >real_infownd_size ){
double calc_amount = (double)msg_length/(double)real_infownd_size;
int msg_amount = (int)ceil(calc_amount);
//char msg[msg_amount][INFOWND_SIZE];

cout<<endl<<"length: "<<msg_length<<endl;
cout<<"MAXIMUM: "<<INFOWND_SIZE-4<<endl;
cout<<"amount of divs: "<<msg_amount<<endl;
}
}

int main(){
new_message( "thisshouldbeapproxmorethan107charactersandwhendividedwithwindowsizeitshouldgiveapproximatll3atround");
return 0;
}``````

EDIT: Oh, forgot to use std::, on that thing, anyway, I implemented it in my function and nothing changed.

Here's the error messages:
1>(src)(122) : error C2057: expected constant expression
1>(src)(122) : error C2466: cannot allocate an array of constant size 0
1>(src)(122) : error C2133: 'msg' : unknown size

... size_t is a standard type (~100% of the time it's an unsigned int)... that's weird. Could you copy and paste what your compiler is saying please? :)

What archaic compiler are you using? Hehe. Get GCC, it supports (practically) all the C++ features + a bit more.

Pff! I use the one Visual C++ Express offer, so far worked perfectly...

Also, it allowed size_t but the errors remained the same

"size_t msg_amount = (int)ceil(calc_amount);"

THE PROBLEM IS WHY DOESN'T THE CHAR ARRAY TAKE MY NEWLY EVALUATED INT VARIABLE?!?!!??! XD

Still waiting for an answer, idea, suggestion... anything that might explain these errors. See above posts.

It doesn't make sense to me. I pass an int inside the char array like so:

double whatever = (double)bla/(double)bla2;
int int_whatever = (int)ceil(whatever);
char stringster[int_whatever][80];

I know that's the problem, and MSVC++ is sometimes quite archaic when it comes to "C code". You can try and use the vector class? MSVC++ apparently can't create 2D "standard" arrays dynamically.

You can of course create the array yourself with new, see:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16

And bookmark that website. Lots of useful info.

So far, everything is very confusing, but the code looks awfully similar to the one you posted earlier...
So, basically what I'm trying to say is, is there no easy way to do what I want it to do? :S
maybe I can use a pointer or something...

what is this vector class you mentioned?

what is this vector class you mentioned?

Short version: STL's resizable arrays.

Long version: I hope you realize I'm not superior to www.cplusplus.com or any reference on the STL when it comes to questions like this right? ;)

If you get stuck on vectors, create a new thread about vectors, but I don't think you'll get stuck on it. It's a great and easy to use class.

You know, I just found out that IT IS POSSIBLE to do it my way, but it wont accept the way I calculate the "int"

I tested it with this:

``````const int test1 = 10/5; //even number without having to round up

double test2_calc = (double)10/(double)5;
const int test2 = (int)test2_calc;

char mychar[test1][ANOTHER_CONSTANT]; //Works
char mychar[test2][ANOTHER_CONSTANT]; //Doesn't work``````

So, in light of this new information. How do I BEST CALCULATE that?

like, rounding up without having to convert to double?

I also think I need a lecture on casts, 'cause maybe I'm not handling them right...

You can only do that when they are defined as constants, so MSVC++ can create the 2D arrays at compile time rather then at runtime.

I suggest reading how to make your own 2D arrays, or using vectors.

Why are you using old casting ?

``````double test2_calc = [B](double)[/B]10/[B](double)[/B]5;
const int test2 = [B](int)[/B]test2_calc;``````

It's a better practice to use the new casting operators to do conversion between datatypes:

``````double num = 5.23;
int num2 = [B]static_cast<int>([/B]num[B])[/B];``````

``````double num = 5.23;
int num2 = [B](int)[/B]num;``````

Boils down to C-style casts being static_casts or reinterpret_casts. But I never got reinterpret_cast to work anyway.

See, now this is why I love these forums! You are all willing to share your knowledge (albeit most knowledge passed on to you by others as well). I didn't know about static_cast, because I rely most of my work on the C++ Beginner's Guide written by the Microsoft Corporation, I guess it's not up to date... either that or I missed static_cast entirely :)

Still tho, the answer to my original question remains unclear, well, I did get some suggestions from Clockowl about vectors, but they were much too confusing and I would ask you to explain them in a more easier way for me to understand, but that is for another thread.

At one time I thought about using pointers, during these 2 days since I posted my problem I have grown to learn more about the power of pointers. I managed to create a pretty good converter function, more specifically a Char To String, AND, a String to Char! I don't know if it already exists or is unnecessary, but it sure was fun! :D

Where was I?... right, thanks to the both of you, especially Clockowl! You seem very active. +rep (if my rep matters)

Be a part of the DaniWeb community

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