Thread: inputting multimensional arrays

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    12

    Unhappy inputting multimensional arrays

    my assignment is a phone book of sorts, i have to input number, store them to a file and so on. how i'm assuming would be the easiest way to do this is using a 2D array. the user needs to be able to input how ever many names they want (up to 1000)i'm declaring the arrays as such-
    [B]char lname[][1000];[\B]
    but i'm not sure how to input them in because when i use-
    [B]scanf("%s", lname[][1000]);[\B]
    it doesn't work. but if i put a number in the first braket, it will only store 1 letter of the name right? thanks for the help

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    I would make the first number of the array the number of names you want to store then the size of the name array(in this case it is unsized), like this:
    Code:
    char lname[1000][];
    Then to have the user input names to the array I would use:
    Code:
    cin.get(lname, 20/*int telling how many characters you will allow to be assigned to the char array*/);
    cin.get doesn't cut off the spaces, of course I assume lname means last name so you would really shouldn't need spaces, but it will work just as well. Hope this helps you!

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try a structure for the records. Keep all the data togather as you collect it.

    Say
    Code:
    typedef struct
    {
            char	Surname[64];
            char	Firstname[64];
            char	Address[128];
    	long    PhoneNum;
    }  PHONE_RECORD_STRUCT;
    
    //for non dynamic array
    PHONE_RECORD_STRUCT  PhoneNum[1000];
    If you have done it use dynamic memory allocation with malloc(), calloc() realloc() ect. Allocate one record and as more are added, realloc another.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    we were told we could use dynamic memory allocation, but i really don't know how to do it in this situation (we havn't gone over it much), could you give me an example or something? thanks.

    oh, and we've never used structures, so thats new to me

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    If you are not using a struct I would not worry about the dynamic mem allocation.

    Using the struc declared before
    Code:
    //declare a pointer to the data type needed 
    PHONE_RECORD_STRUCT  *pPhoneNum;
    //malloc the first record
    pPhoneNum=(PHONE_RECORD_STRUCT *) malloc( sizeof (PHONE_RECORD_STRUCT) );
    
    //fill in first record, when asked for second record iNumRecords++;
    
    //realloc for next record
    // iNumRecords is the number of records
    pPhone=(PHONE_RECORD_STRUCT *) realloc( pPhone, sizeof (PHONE_RECORD_STRUCT) * iNumRecords );
    
    //To input record details, iIndex is zero based array reference (0 to iNumRecords-1)
    pPhone[iIndex].PhoneNum=lInputBuffer;
    sprintf(pPhone[iIndex].SurName,"%s",sInputBuffer);
    //ect
    But I would not worry about this at the moment.
    Use
    #define MAX_RECORDS 1000//handy number to define
    char InName[MAX_RECORDS][]; //or similar
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > #define MAX_RECORDS 1000//handy number to define
    > char InName[MAX_RECORDS][]; //or similar

    I'd opt for the 'or similar' approach:

    char *InName[MAX_RECORDS];

    Read the name into a buffer, then allocate space for it and copy it there.

    InName[x] = malloc( strlen( buffer ) +1 );

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Just to clarify for BR7

    >> strlen( buffer ) +1 (amount of memory to allocate)

    char buffer[64]={"hello\0"};

    strlen(buffer)=5 [not 64 or 6 (misses out the '\0')]

    strlen() returns the length without the terminator '\0' so need to add 1 to ensure enough space

    As each string (element) of the array (ie InBuffer[2] or InBuffer[3]) has no memory each must be malloc()'ed individually as used.

    realloc() is not needed unless the element that has already been malloc()'ed is changed and more (or less) memory is needed [realloc() can only be used on an element that has been previously alloc()'ed]

    can simply do without any dynamic memory allocation

    char InBuffer[MAX_RECORDS][64];//less than 64Kb of mem
    Last edited by novacain; 02-22-2002 at 02:49 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM