By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Help as soon as possible, please?

P: 3
Here's my dilemma: I'm writing a program that compares and displays the highest and lowest elements in a user-defined array. Thing is, this array can be as large or as small as the user wants. My problem is getting the size of the array to vary.

This is what I have coded:

int main()
{
int temp = 0;
cout << "How many numbers are in the data set?\n";
cin >> temp;

const int SIZE = temp;
double largesmall[SIZE];
....


and this is the error I get:

File1.C(57); E2313 Constant Expression Required

How do I fix this?
Nov 13 '06 #1
Share this Question
Share on Google+
7 Replies


10K+
P: 13,264
Here's my dilemma: I'm writing a program that compares and displays the highest and lowest elements in a user-defined array. Thing is, this array can be as large or as small as the user wants. My problem is getting the size of the array to vary.

This is what I have coded:

int main()
{
int temp = 0;
cout << "How many numbers are in the data set?\n";
cin >> temp;

const int SIZE = temp;
double largesmall[SIZE];
....


and this is the error I get:

File1.C(57); E2313 Constant Expression Required

How do I fix this?
Illegal assignment to const variable
why not just do
Expand|Select|Wrap|Line Numbers
  1.  cin >> temp; 
  2.  
  3. double largesmall[temp];
  4.  
Nov 13 '06 #2

P: 3
Illegal assignment to const variable
why not just do
Expand|Select|Wrap|Line Numbers
  1.  cin >> temp; 
  2.  
  3. double largesmall[temp];
  4.  
Tried that, same error.

I should also probably mention that I'm using Borland C++ Builder 6 to compile my code.
Nov 13 '06 #3

10K+
P: 13,264
Tried that, same error.

I should also probably mention that I'm using Borland C++ Builder 6 to compile my code.
Same error?
Expand|Select|Wrap|Line Numbers
  1.  
  2. int main()
  3. {
  4. int temp = 0;
  5. cout << "How many numbers are in the data set?\n";
  6. cin >> temp;
  7. double largesmall[temp];
  8.  
  9.  
Are you sure that
1) You have save your changes
2)the error is occuring on the same line
Nov 13 '06 #4

P: 3
Same error?
Expand|Select|Wrap|Line Numbers
  1.  
  2. int main()
  3. {
  4. int temp = 0;
  5. cout << "How many numbers are in the data set?\n";
  6. cin >> temp;
  7. double largesmall[temp];
  8.  
  9.  
Are you sure that
1) You have save your changes
2)the error is occuring on the same line
Here's the funny thing. Tried the exact same code at school (KDE Linux, using nedit to write and G++ to compile.) and it worked just fine. I think it's just Borland that doesn't like the code. Another strike for doing my coding at home. *sigh*

Thanks for the help at any rate. *laughs*
Nov 13 '06 #5

100+
P: 145
Here's the funny thing. Tried the exact same code at school (KDE Linux, using nedit to write and G++ to compile.) and it worked just fine. I think it's just Borland that doesn't like the code. Another strike for doing my coding at home. *sigh*

Thanks for the help at any rate. *laughs*
I'd just do something like this:

Expand|Select|Wrap|Line Numbers
  1. int ArraySize;
  2. double *data;
  3.  
  4. cout << "Input size of array: ";
  5. char InputString [1024];
  6. cin >> InputString;
  7.  
  8. ArraySize = atoi( InputString );
  9. data = new double [ArraySize];
  10.  
  11. // get min and max from here.
  12.  
Off-topic: Is there some reason you're using Borland's compiler? I've always found it to be pretty awful, dated, and not great at being standards-compliant. You might want to consider looking at mingw, a windows-native port of g++. -- Paul
Nov 13 '06 #6

Ganon11
Expert 2.5K+
P: 3,652
The problem is that most C++ compilers need a constant expression to make a normal array. They need a hard-coded number like 5 to be able to realize exactly how much memory to allocate. If you use a variable, the compiler really doesn't know, and can't tell if there's enough space to allocate the array. Even if you make a constant variable, if you set it to another, dynamic variable, the compiler doesn't know. There are two ways to solve this.

1) Make an array of a ridiculously large size, like 10000. Ask the user for the number of elements, and only use that number of elements. For example...

Expand|Select|Wrap|Line Numbers
  1. ...
  2. int myArray[10000];
  3. int size;
  4. cout << "How many values? ";
  5. cin >> size;
  6.  
  7. for (int i = 0; i < size; i++) {
  8.    ...
  9.    ...
  10.    ...
  11. }
However, this leaves a lot of computer space wasted, unused.

2) Use a pointer array. Get the user's size, then declare the array like this:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. int* myArray[size];
  3. ...
and treat as normal.
Nov 14 '06 #7

100+
P: 145
The problem is that most C++ compilers need a constant expression to make a normal array. They need a hard-coded number like 5 to be able to realize exactly how much memory to allocate.
I think I'm misunderstanding you here, but C++ has allowed dynamic memory allocation for years.

1) Make an array of a ridiculously large size, like 10000. Ask the user for the number of elements, and only use that number of elements.
This sounds like the Fortran way. :)

2) Use a pointer array. Get the user's size, then declare the array like this:
This is what I recommended in my previous post. ;) -- Paul
Nov 14 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.