Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. *** Declaring Array size from User Input ***

*** Declaring Array size from User Input ***

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Lai
    wrote on last edited by
    #1

    Hi Everyone, I would like to use an array that has a size that is determined by the user input. But I'm getting this problem of " array must contain constant " from this array declaration: int UserInput; int TheArray[UserInput]; How do I declare the size of the array with user input?? if anyone knows, Please let me know. Thanks in Advance!

    L P 2 Replies Last reply
    0
    • S Steve Lai

      Hi Everyone, I would like to use an array that has a size that is determined by the user input. But I'm getting this problem of " array must contain constant " from this array declaration: int UserInput; int TheArray[UserInput]; How do I declare the size of the array with user input?? if anyone knows, Please let me know. Thanks in Advance!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      1. If it is a maximum possible array size, you can declare array of this size, like this: #define MAX_USER_INPUT 255 int TheArray[MAX_USER_INPUT]; Do not forget to check UserInput in this case. if (UserInput > MAX_USER_INPUT) // show error message 2. Or use new() and delete() int *TheArray = NULL; BOOL InitArray(int *&TheArray, int UserInput) { if (UserInput <= 0) return FALSE; if (TheArray) delete[] TheArray; TheArray = new int[UserInput]; return TheArray != NULL; } Don't forget to delete[] TheArray.

      1 Reply Last reply
      0
      • S Steve Lai

        Hi Everyone, I would like to use an array that has a size that is determined by the user input. But I'm getting this problem of " array must contain constant " from this array declaration: int UserInput; int TheArray[UserInput]; How do I declare the size of the array with user input?? if anyone knows, Please let me know. Thanks in Advance!

        P Offline
        P Offline
        Philip Nicoletti
        wrote on last edited by
        #3

        int theUserInput; theUserInput = 10; // or from user input int *array; array = new int[theUserInput]; array[0] = 5; // whatever

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups