Small problem I need help with.

This is a discussion on Small problem I need help with. within the C++ Programming forums, part of the General Programming Boards category; Part of the program creates objects of the class HUMAN and creates pointers to these objects. These pointers are the ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Small problem I need help with.

    Part of the program creates objects of the class HUMAN and creates pointers to these objects. These pointers are the stored in an array. Anyway the user inputs the number of family members it wants to create and this number is used to specify the length of the human array.

    Code:
    int human_max;
    std::cout << "\n\nPlease input the number of family members you would like to make: ";
    std::cin >> human_max;
    HUMAN *human_array[human_max];
    HUMAN* ptr_h;
    The compiler says a constant expression is required. The program works fine if I replace it with

    Code:
    HUMAN *human_array[10]
    but wont accept the user input number.

    How can I solve this problem?

    Thanks in advance.

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    An array needs a fixed size, so at compile time it needs a fixed size.

    But you can create a dynamic array using pointers (that is, the size can be chosen at run-time) by using the new command (that is, it allocates the memory for the array during run time, and thus the user can decide the size of the array).

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    And just search google for “c++ dynamic array” for more info.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    std::vector<Human> humans( number_of_humans );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 10:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 03:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 03:23 AM

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