| | |
"expect a constant expression"
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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,
Anything unclear? eh.. let's take a look...
Please try and give a simple answer that I can understand.
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
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.
Anyone have an answer as to why it gives me an error: "Expected a constant expression at msg"
Here's the code,
C++ Syntax (Toggle Plain Text)
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.

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
C++ Syntax (Toggle Plain Text)
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; } }
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.
Last edited by ShadowScripter; Apr 17th, 2009 at 8:24 pm.
Does this code compile for you?
cpp Syntax (Toggle Plain Text)
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
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
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
cpp Syntax (Toggle Plain Text)
#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
Last edited by ShadowScripter; Apr 17th, 2009 at 8:52 pm.
... 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? 
size_t is:
http://www.cplusplus.com/reference/c...string/size_t/

size_t is:
http://www.cplusplus.com/reference/c...string/size_t/
Last edited by Clockowl; Apr 17th, 2009 at 8:32 pm.
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
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
Last edited by ShadowScripter; Apr 17th, 2009 at 8:46 pm.
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];
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];
Last edited by ShadowScripter; Apr 17th, 2009 at 9:10 pm.
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-lit...html#faq-16.16
And bookmark that website. Lots of useful info.
You can of course create the array yourself with new, see:
http://www.parashift.com/c++-faq-lit...html#faq-16.16
And bookmark that website. Lots of useful info.
Last edited by Clockowl; Apr 17th, 2009 at 9:10 pm.
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?
maybe I can use a pointer or something...
what is this vector class you mentioned?
So, basically what I'm trying to say is, is there no easy way to do what I want it to do?

maybe I can use a pointer or something...
what is this vector class you mentioned?
Last edited by ShadowScripter; Apr 17th, 2009 at 9:30 pm.
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.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Major Problem with Doubly linked list program
- Next Thread: Trouble with multi-file program
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





