letting user input number of elements in array

This is a discussion on letting user input number of elements in array within the C++ Programming forums, part of the General Programming Boards category; can a user input the number of elements in an array? this won't work: Code: int liEls; cout << "Elements ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    Question letting user input number of elements in array

    can a user input the number of elements in an array? this won't work:

    Code:
    int liEls;
    cout << "Elements in array liaArray[]="; cin >> liEls;
    const int cliEls=liEls;
    int liaArray[cliEls];
    ^^note cliEls versus liEls.

    i'm trying to learn c++, thanks for your help!

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    28
    Sure:

    Code:
    int nArraySize = 0;
    
    cout << "How many items in the array? ";
    cin >> nArraySize;
    
    int *pnArray = (int *)calloc(nArraySize, sizeof(int));
    
    // Use pnArray as a normal array...
    
    free(pnArray);

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    (int *)???
    calloc() ???
    free() ??
    Did you see the sign on the door when you came in? This is a C++ forum.
    ---------

    ^^note cliEls versus liEls.

    i'm trying to learn c++
    First lesson: use simple, clear variable names. It appears that picking non-confusing variable names is not your strong suit at this point, so stick with single letters of the alphabet until you get the hang of it.

    C++ allows you to dynamically create arrays at runtime. Here is how it is done:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	int s;
    	cout<<"Enter the array size: ";
    	cin>>s;
    
    	string* ptr = new string[s];
    	
    	for(int i = 0; i < s; i++)
    	{
    		cout<<"Enter some text: ";
    		cin>>ptr[i];
    	}
    
    	for(int j = 0; j< s; j++)
    	{
    		cout<<ptr[j]<<endl;
    	}
    
    	delete [] ptr;
    		
       return 0;
    }
    Last edited by 7stud; 05-22-2005 at 01:41 AM.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    28
    -_- I'm just stuck in the past. :P

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    calloc / free is C, this is C++

    So
    Code:
    int nArraySize = 0;
    
    cout << "How many items in the array? ";
    cin >> nArraySize;
    
    int *pnArray = new int[nArraySize];
    
    // Use pnArray as a normal array...
    
    delete [] pnArray;
    Did I mention I dislike hungarian notations?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The "best" way to allocate an array based on user input is to use a vector:
    Code:
    int liEls;
    cout << "Elements in array liaArray[]="; cin >> liEls;
    std::vector<int> liaArray(liEls);
    There is no need to cleanup (delete or free) the memory later, it will be cleaned up automatically like your original static array is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  5. How to let the user input values for an array...
    By TerranAce007 in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2002, 03:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21