Thread: Size of Array, user inputted..

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    I wish to go the route of a maxinum array..

    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    
    int length;
    int list[100];
    int counter;
    
    
    int main()
    {
    
    
    std::cout << "How many numbers do you wish to input?: ";
    std::cin >> length;
    
    for (counter = 0; counter < length; ++counter)
            {
            std::cout << "Number? ";
            std::cin >> //***ARRAY ELEMENT X
    
            }
    
    
    //***   ONCE THE ARRAY IS FILLED WITH THE USER DEFINED AMOUNT, I NEED
            TO SEARCH THE ARRAY FOR A USER INPUTTED NUMBER, AND OUTPUT HOW
            MANY TIMES IT IS IN THE ARRAY
    ***//
    
    
    
    
    return (0);
    }
    Here, I'm hoping to have a user input length, and then use a FOR statement to prompt the user length times to input numbers into the array... How do I code a FOR statement to fill an array element with the next slot each time?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you really want to go the dynamic memory allocation route then you've got some switching around of code to do to get it working properly:
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    int main()
    {
        int length;
    
        std::cout << "How many numbers do you wish to input?: ";
        std::cin >> length;    // Get length from user "before" you attempt to allocate space
    
        // Now allocate space based on user input length...
        int * list = new int[length];
    
       
        // Do whatever you need to with the list array you've allocated.
    
    
        // Clean up the memory you've allocated.
        delete [] list;
    
        return 0;
    }
    Using a vector is far better like others have mentioned... you don't need to new/delete any memory, it's automatically done for you by the vector itself.

    The algorithm header you are including contains a templated function that can easily do the counting for you (not gonna tell you what it's called) although I suspect you need to learn/experience loops more.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Assign array size by user
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2001, 06:45 AM