"expect a constant expression"
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.
ShadowScripter
Junior Poster in Training
94 posts since Apr 2009
Reputation Points: 12
Solved Threads: 6
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
ShadowScripter
Junior Poster in Training
94 posts since Apr 2009
Reputation Points: 12
Solved Threads: 6
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
ShadowScripter
Junior Poster in Training
94 posts since Apr 2009
Reputation Points: 12
Solved Threads: 6
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];
ShadowScripter
Junior Poster in Training
94 posts since Apr 2009
Reputation Points: 12
Solved Threads: 6
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?
ShadowScripter
Junior Poster in Training
94 posts since Apr 2009
Reputation Points: 12
Solved Threads: 6
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...
ShadowScripter
Junior Poster in Training
94 posts since Apr 2009
Reputation Points: 12
Solved Threads: 6
Why are you using old casting ?
double test2_calc = <strong>(double)</strong>10/<strong>(double)</strong>5;
const int test2 = <strong>(int)</strong>test2_calc;
It's a better practice to use the new casting operators to do conversion between datatypes:
double num = 5.23;
int num2 = <strong>static_cast<int>(</strong>num<strong>)</strong>;
instead of the old ones:
double num = 5.23;
int num2 = <strong>(int)</strong>num;
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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)
ShadowScripter
Junior Poster in Training
94 posts since Apr 2009
Reputation Points: 12
Solved Threads: 6