473,387 Members | 3,033 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,387 software developers and data experts.

variable array size (constant problem)

I have a program that asks a user for a integer. This integer is a member of a struct statement. Later on in the program a function points to this structure in hopes of using the integer as a variable in an array. The problem that I'm seeing is that the complier, and likely the c++ coding standard, is looking for a constant to be decalared at compile. Am I thinking in the wrong way? I want to make an array that depends on the size of the input. If anyone has a moment to help me think out of the box, I would be most appreciative. Thanks all.
Aug 5 '06 #1
6 13737
After looking at my question I noticed that I wasn't being clear enough. I want to define array size with the input integer. Sorry bout that.
Aug 5 '06 #2
D_C
293 100+
Define a pointer to the array. Then, later in the program when you know how big it needs to be, then you can define a constant size array, and point your pointer to it.

If the size is dynamic and will keep changing, I suggest you use a vector. It is already implemented in STL.
Aug 6 '06 #3
Thank you for your response. The workaround that I figured was to set a constant high enough that it would be unlikely (yes I know....) that the user would ever use that variable. I then set my count variable to the size determined by user input. So I'm wasting memory, but it worked. I like your suggestion better as it seems more efficient. This program is for my final in an intro to c++ class. Thanks again!

Joshua
Aug 6 '06 #4
In short you want to create the dynamic array
You can take approach of singly linked list here
Aug 11 '06 #5
Well, I am in the same boat. I found these forums while trying to find a way to get the size of an array from user input.

I haven't done any programming I took a class in it last year, and am trying to practice so I don't get owned in my 'programming 2' class.


What I have is something like this:


cout << "Please enter the number of salespeople/n";
int n;
cin >> n;


I want to size of the array to be 'n'. This happens at the beginning of int main().


The size of the array doesn't need to change after this. I just want it to be based on the user's input.


I didn't understand D_C's explanation at all. :/ We didn't do pointers in my first class, although we will cover them in this one. I have tried reading my book and while I sort of understand what a pointer is, I still can't apply it to my problem.

Any help or clarification will be much appreciated.
Aug 24 '06 #6
D_C
293 100+
Expand|Select|Wrap|Line Numbers
  1.   int LENGTH;
  2.   int* array;
  3.  
  4.   cout << "Enter number of array entries: ";
  5.   cin >> LENGTH;
  6.   cout << endl;
  7.  
  8.   array = (int*)malloc(LENGTH*sizeof(int));
  9.  
  10.   int i;
  11.   for(i = 0; i < LENGTH; i++)
  12.   {
  13.     cout << "Enter an array value: ";
  14.     cin >> array[i];
  15.   }
  16.  
  17.   for(i = 0; i < LENGTH; i++)
  18.     cout << array[i] << " ";
  19.   cout << endl;
Aug 24 '06 #7

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

Similar topics

6
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid =...
18
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
4
by: Piotr Sawuk | last post by:
Hello, I'm new in this group and new to c++ programming. And I already have my first question which wasn't answered by any text-book on c++ programming I have seen so-far: How can I define a...
13
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
6
by: JNY | last post by:
Hello, Is it possible to declare an array with variable indeces? i.e. int x = 4; int myArray; for (j = 0;j < 5;j++) {
3
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
7
by: Cromulent | last post by:
In section 6.7.5.2 it states the following: If the size is not present, the array type is an incomplete type. If the size is*instead of being an expression, the array type is a variable length...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.