943,648 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2031
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 17th, 2009
0

"expect a constant expression"

Expand Post »
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,

C++ Syntax (Toggle Plain Text)
  1. const int INFOWND_SIZE = 4;
  2. int msg_length[] = "Message";
  3.  
  4. double calc_amount = (double)msg_length/(double)INFOWND_SIZE;
  5.  
  6. int msg_amount = (int)ceil(calc_amount);
  7.  
  8. 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)
  1. const int INFOWND_SIZE = 51;
  2.  
  3. void new_message(char var[]){
  4. COORD coord = {(infoffset.X+1), (infoffset.Y+1)};
  5. int real_infownd_size = INFOWND_SIZE-4;
  6. int msg_length = strlen(var);
  7. if( msg_length >real_infownd_size ){
  8. double calc_amount = (double)msg_length/(double)real_infownd_size;
  9. int msg_amount = (int)ceil(calc_amount);
  10. char msg[msg_amount][INFOWND_SIZE];
  11.  
  12. cout<<endl<<"length: "<<msg_length<<endl;
  13. cout<<"MAXIMUM: "<<INFOWND_SIZE-4<<endl;
  14. cout<<"amount of divs: "<<msg_amount<<endl;
  15. }
  16. }
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.
Last edited by ShadowScripter; Apr 17th, 2009 at 8:24 pm.
Reputation Points: 12
Solved Threads: 6
Junior Poster in Training
ShadowScripter is offline Offline
94 posts
since Apr 2009
Apr 17th, 2009
0

Re: "expect a constant expression"

Does this code compile for you?

cpp Syntax (Toggle Plain Text)
  1. int main(){
  2. size_t cols = 10;
  3. cin >> cols;
  4. size_t rows = 20;
  5. cin >> rows;
  6.  
  7. char x[cols][rows];
  8. for(int i = 0; i < rows; i++){
  9. for(int n = 0; n < cols; n++){
  10. cout << x[i][n];
  11. }
  12. cout << endl;
  13. }
  14.  
  15.  
  16. return 0;
  17. }
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 17th, 2009
0

Re: "expect a constant expression"

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

cpp Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <conio.h>
  5. #include <windows.h>
  6. #include <string.h>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. const int INFOWND_SIZE = 51;
  12.  
  13. void new_message(char var[]){
  14. int real_infownd_size = INFOWND_SIZE-4;
  15. int msg_length = strlen(var);
  16. if( msg_length >real_infownd_size ){
  17. double calc_amount = (double)msg_length/(double)real_infownd_size;
  18. int msg_amount = (int)ceil(calc_amount);
  19. //char msg[msg_amount][INFOWND_SIZE];
  20.  
  21. cout<<endl<<"length: "<<msg_length<<endl;
  22. cout<<"MAXIMUM: "<<INFOWND_SIZE-4<<endl;
  23. cout<<"amount of divs: "<<msg_amount<<endl;
  24. }
  25. }
  26.  
  27. int main(){
  28. new_message( "thisshouldbeapproxmorethan107charactersandwhendividedwithwindowsizeitshouldgiveapproximatll3atround");
  29. return 0;
  30. }

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.
Reputation Points: 12
Solved Threads: 6
Junior Poster in Training
ShadowScripter is offline Offline
94 posts
since Apr 2009
Apr 17th, 2009
0

Re: "expect a constant expression"

... 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/
Last edited by Clockowl; Apr 17th, 2009 at 8:32 pm.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 17th, 2009
0

Re: "expect a constant expression"

What archaic compiler are you using? Hehe. Get GCC, it supports (practically) all the C++ features + a bit more.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 17th, 2009
0

Re: "expect a constant expression"

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
Last edited by ShadowScripter; Apr 17th, 2009 at 8:46 pm.
Reputation Points: 12
Solved Threads: 6
Junior Poster in Training
ShadowScripter is offline Offline
94 posts
since Apr 2009
Apr 17th, 2009
0

Re: "expect a constant expression"

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];
Last edited by ShadowScripter; Apr 17th, 2009 at 9:10 pm.
Reputation Points: 12
Solved Threads: 6
Junior Poster in Training
ShadowScripter is offline Offline
94 posts
since Apr 2009
Apr 17th, 2009
0

Re: "expect a constant expression"

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.
Last edited by Clockowl; Apr 17th, 2009 at 9:10 pm.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 17th, 2009
0

Re: "expect a constant expression"

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?
Last edited by ShadowScripter; Apr 17th, 2009 at 9:30 pm.
Reputation Points: 12
Solved Threads: 6
Junior Poster in Training
ShadowScripter is offline Offline
94 posts
since Apr 2009
Apr 17th, 2009
0

Re: "expect a constant expression"

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Major Problem with Doubly linked list program
Next Thread in C++ Forum Timeline: Trouble with multi-file program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC