CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    4

    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....

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple Array Query

    While std::vector is the recommended way to do this, you should also know how to use operator new[].

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simple Array Query

    Quote Originally Posted by Lindley View Post
    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

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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;
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured