-
December 10th, 2008, 03:24 PM
#1
Simple Array Query
Hello! I know this is probably very easy and quite obvious but I just can't seem to find the answer anywhere and so thought perhaps someone here could help me. I am working on a program which asks the user to tell it how many values are to be stored in the array and then the program asks for each value in turn. I know how to ask for each value, but not sure how to create an array with a changing amount of data to be held in it.
This is a simplified version of my array to explain it better...
Code:
int age[5];
age[0]=12;
age[1]=14;
age[2]=18;
age[3]=40;
How do I get the user to change the number of values in the array - from the 5 to say 3 or 9?
Many thanks, I'm sure it's very easy! Just having a bad day....
-
December 10th, 2008, 03:34 PM
#2
Re: Simple Array Query
Arrays are fixed size. Do yourself a big favor and use the appropriate STL containers, easier, more reliable, more powerful. In this case std::vector<int>
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 10th, 2008, 03:38 PM
#3
Re: Simple Array Query
While std::vector is the recommended way to do this, you should also know how to use operator new[].
-
December 10th, 2008, 03:42 PM
#4
Re: Simple Array Query
Originally Posted by Lindley
While std::vector is the recommended way to do this, you should also know how to use operator new[].
Lindley is correct. ...
But as soon as you call new[], your need to also understand operator delete [] (remembering to always pair new/delete and new[]/delere[]) and robust exception handling. Each of these is REQUIRED if you are going to be using dynamic naked arrays....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 11th, 2008, 02:23 AM
#5
Re: Simple Array Query
Here is a tutorial for vector: http://www.codeguru.com/Cpp/Cpp/cpp_...icle.php/c4027.
How do I get the user to change the number of values in the array - from the 5 to say 3 or 9?
Like this:
Code:
int size = 0;
cout << "Array size: ";
cin >> size;
int* array = new int[size];
...
delete [] array;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|