473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Help as soon as possible, please?

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
7 2383
r035198x
13,262 8TB
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
BigMB
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
r035198x
13,262 8TB
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
BigMB
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
macklin01
145 100+
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
3,652 Expert 2GB
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
macklin01
145 100+
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

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

Similar topics

5
by: anthonyberet | last post by:
I work for an organisation that uses a bespoke document imaging system, the database of which is an MS sql server. We have MS Access and already use it for some querying of the database. The...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
4
by: kirill_uk | last post by:
Help with extracting please folks.! Hi. I have this: a variable like: <a href="http://www.some_html.com/text.html" >some text</a><br> I heed to extract the "http://www.some_html.com/text.html "...
1
by: Jay Zweedyk | last post by:
Ok I want to filter a dataset into a dataview and be able to reference back to the dataset from the filtered dataview. Example: 100 record dataset filter it to a 5 record dataview loop...
0
by: Wildemar Wildenburger | last post by:
Ok, so i've whipped up this GUI with wxPython but I realize that it might hinder the sort of thing I wantto do. What do I want to do? Basically its a note taking app that should present little...
29
by: L1sa | last post by:
Hi, I am guessing this is something simple to you guys however I am new to sql and am unsure how to create this query (I must do it in SQL). I am wanting to show in one query which...
4
cyberking
by: cyberking | last post by:
Hi, I have a website http://www.projectvtu.com Now this website has frames in all of its pages. The problem I am facing now is that the website works very well on my subnet at home. But, I...
8
by: Hugh Newbury | last post by:
Hi: My two websites date from the Ark, and I would like to upgrade/start again. Please advise me where to go to get the latest reliable reference to HTML etc. I'd prefer a book: my last buy was...
2
by: ganapathidev | last post by:
Iam Ganapathi, Iam preparing registration form for that i need 2 pints. 1.) List of world languages in html code. 2.) List of Indian Languages in html code. please send me replay as soon as...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.