Thread: Using a multidimensional array to store a list of first and last names

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    Using a multidimensional array to store a list of first and last names

    3. Create a program that allows a user to enter up to five names
    of friends. Use a two-dimensional array to store the friends’
    names. After each name is entered, the user should have the
    option to enter another name or print out a report that shows
    each name entered thus far.

    I have some code that I posted on here a long time ago its a close thread now anyway I'm just wondering how to print out say 5 names first and last here's the old code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
     
        int i = 0;
        char *iName[2][15] =  {'\0'};
        int iChoice = 0;
     
     
        while(iChoice != 3)
        {
            system("cls");
            printf("\n1\tEnter a name");
            printf("\n2\tPrint Report");
            printf("\n3\tQuit");
            printf("\n\n\tEnter a Choice: ");
            scanf("%d", &iChoice);
     
            if(iChoice == 1)
            {
                system("cls");
                printf("\n\tEnter a name: ");
                scanf("%s %s", iName[0],iName[1]);
            }
            else if(iChoice ==2)
            {
                system("cls");
                for (i = 0; i <= 3; i++)
                  printf("%s %s\n",iName[i],iName[i]);
                break;
            }
        }
    
        system("Pause");
        return (0);   
    
     
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    char *iName[2][15] =  {'\0'}; // Drop the '*' you can do this by having just a 2d array of characters
    Ask yourself how you would enter in 5 integers into an array.. You would use a loop. Well this is the same type of deal. You are going to need more rows in your array if you want to be able to enter in 5 names. Right now you can only enter in 2 names.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by camel-man View Post
    Code:
    char *iName[2][15] =  {'\0'}; // Drop the '*' you can do this by having just a 2d array of characters
    Ask yourself how you would enter in 5 integers into an array.. You would use a loop. Well this is the same type of deal. You are going to need more rows in your array if you want to be able to enter in 5 names. Right now you can only enter in 2 names.
    Eh, the two is for first name and last name. The 15 is the number of names.

    The fun part is that we have a two-dimensional array of char pointers, which currently point to nowhere. Memory will have to be allocated for each name before things can actually be stored.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    char *iName[2][15] =  {'\0'};
    What would this be considered? A 2d array of all pointers to char? Does that mean that there are 30 dangling pointers to type char?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by camel-man View Post

    What would this be considered? A 2d array of all pointers to char? Does that mean that there are 30 dangling pointers to type char?
    Pretty much, yeah. Isn't it exciting?

    (EDIT: I suppose I should add that having a 2d array of pointers to char is not itself actually a bad thing (depending on how this array is going to get used) -- but not realizing that that is what it is can lead to very bad things indeed.)
    Last edited by tabstop; 10-13-2013 at 10:11 PM.

  6. #6
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    maybe this will help you,

    char *name[];

    string constant in indexing array.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    how would I allocated memory for the names to be stored the problem statement wants me to use a multidimensional array so loserone I don't want to use a single dimensiion
    can you guys use laymens terms that I can understand hee hee

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I was just kidding I somewhat understand what you guys are talking about

    I plan on changing the for loop around thats not permanent

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    I still suggest you go with a 2d array rather than a 2d array of pointers. This way you will eliminate the need for dynamic memory allocation which you probably have not gone over yet.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Do you mean like this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
     
        int j = 0;
        int i = 0;
        char iName[2][15] =  {'\0'};
        int iChoice = 0;
     
     
        while(iChoice != 3)
        {
            system("cls");
            printf("\n1\tEnter a name");
            printf("\n2\tPrint Report");
            printf("\n3\tQuit");
            printf("\n\n\tEnter a Choice: ");
            scanf("%d", &iChoice);
     
            if(iChoice == 1)
            {
                system("cls");
                printf("\n\tEnter a name: ");
                scanf("%s", &iName[0]);
            }
            else if(iChoice ==2)
            {
                system("cls");
                
                  for (i = 0; i <= 5; i++)
                    printf("%s\n",iName[i]);
                break;
            }
        }
    
        system("Pause");
        return (0);   
    
     
    }
    This way also doesn't give me the correct print out its suppose to be john Wayne an john Mayer this is the result


    Mayer



    τ◄@

    Press any key to continue . . .

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. This declaration:
      Code:
      char iName[2][15]
      gives you two names of fourteen characters each. You are trying to store four names (John, Wayne, John, and Mayer) and reusing slot 0 each time; hence you will only end up with one name in the list. You will need to keep track, one way or another, of where you are in the list and use that number when inserting names.
    2. If you go with array-of-char (instead of array-of-char-*) then your two-dimensional array is really just a one-dimensional array of strings (since a one-dimensional array-of-char is itself just a single string). If you want a string for last name and a string for first name, you'll need another dimension.
    3. As a follow-up, if you do go with the entire name in one slot, then you cannot use %s to read in the names, since it will stop on a space -- check up on your other input methods. If you do go with separate slots for first names and last names, you'll have to decide what you want to do with Robert Penn Warren.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with strcpy() in multidimensional linked list
    By immie86 in forum C Programming
    Replies: 24
    Last Post: 05-26-2009, 01:25 AM
  2. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  3. sort list of names?
    By CwannaB in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2003, 09:53 PM
  4. how do u sort a list of names?
    By CwannaB in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 05:19 PM
  5. Sorting list of names?
    By aspand in forum C Programming
    Replies: 5
    Last Post: 05-16-2002, 11:10 AM