Thread: storing strings into array

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    storing strings into array

    Hi, I am having trouble storing strings into an array. Basically I have a console program where it has an option.

    This is just an example;
    Code:
    char name[max];
    char listofnames[maxname];
    int counter;
    ....
    printf("Add names\n");
    //Then a list of names
    // they can select 
    switch(choice)
    {
    case 1: 
    strcpy(name,"kyle");
    break;
    
    case 2:
    strcpy(name,"phil");
    break;
    
    etc...
    }
    
    //Now in there they have an option wether they like to add more names
    // if they said "yes"
    //      the name chosen should be stored in array listofnames
    // something like
    if(strcmp(selection,"yes")==0)
    {
      listofnames[counter]=name;
    // I AM HAVING TROUBLE STORING IT IN LISTOFNAMES ARRAY
    }
    if(strcmp(selection,"no")==0)
    {
       //printf list of names array
    // AND HOW CAN I PRINT IT?
    
    }
    can anyone please help. thankyou

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where is your array of strings? The listofnames is an array of char not an array of C-strings. If you want an array of C-strings you need a multidimensional array.

    Code:
    char listofnamesname[max][maxname];
    You've already shown you know how to copy strings so why are you doing the following?
    Code:
    listofnames[counter]=name;
    You don't use the assignment operator= with C-strings.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    22
    Hi,

    I managed to store names into listofnames.. how can I print the listofnames array into 1 string?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What have you tried?

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    22
    basically i an store names into an array ..listname[max][maxlength]

    but these names are stored in each array. I am trying to print all these names into 1 line of string

    I tried
    Code:
    for(i = 0; i < counter; i++)
    {
    sprintf(namebuffer"%s",listname[i]);
    
    }
    but it seems wrong because when the first value in listname is loaded ..it is overwritten by the next value in the array

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    As long as namebuffer is large enough to hold all the strings you should be using a combination of strcpy() and strcat(). Copy the first string outside the loop with strcpy() then use strcat() inside the loop. But be sure that namebuffer was defined with a size that is large enough to hold the length of all the strings added together. So if listname is defined as listname[max][maxname] namebuffer must have a size of (max * maxname) + 1.

    Jim

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    22
    so you mean something like..

    Code:
    listname[max][maxname];
    
    strcpy(namebuffer,listname[0]);
    for(i = 0; i < counter; i++)
    
    {
    
       //sprintf(namebuffer"%s",listname[i]);
         strcat(namebuffer,listname[i]);
    
     
    
    }
    how can i set the size of listname as size of (max * maxname) + 1 ?
    like this?
    listname=(char*)malloc(sizeof(max*maxname)+1)); ??

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    so you mean something like..
    Close but:
    Code:
    listname[max][maxname];
     
    strcpy(namebuffer,listname[0]);
    for(i = 1; i < counter; i++) // Start at one, already copied element 0.
    {
         strcat(namebuffer,listname[i]);
    }
    how can i set the size of listname as size of (max * maxname) + 1 ?
    It's not listname that needs to be that size, it't namebuffer. How did you define namebuffer?

    Jim

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    22
    oh yeah its namebuffer instead of litsname

    I defined it like namebuffer[max][maxname];

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then you don't have room for the combined strings. Try something like:
    Code:
    namebuffer[(max*maxname)];

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 04-12-2012, 06:09 AM
  2. Storing several strings in an char array?
    By Mexicouger in forum C Programming
    Replies: 5
    Last Post: 04-04-2011, 03:07 PM
  3. storing strings by enum/s
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2008, 08:03 AM
  4. storing strings into a 2d array
    By ronin in forum C Programming
    Replies: 2
    Last Post: 10-24-2002, 09:36 PM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM