Storing names and email address.

This is a discussion on Storing names and email address. within the C Programming forums, part of the General Programming Boards category; Hi I have tried to make this work, but I can't figure out the logic, I want to write the ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    Storing names and email address.

    Hi

    I have tried to make this work, but I can't figure out the logic, I want to write the function so that names and email addressed like the following are passed to it and it would put the name in a name array and the email address in a email array.

    Example:

    David Jesse dave@hotmail.com
    Rob rob@hotmail.com
    Joe c mark joe@hotmail.com

    so if anything similar to the above examples is passed to the function , the function would put the name in an array and email address in another array. I tried many loops but non of them worked correctly. Any help is appreciated.


    Thank You

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by arya6000 View Post
    I tried many loops but non of them worked correctly.
    Off-topic: This reminds me of a CV I saw a while back from someone who has "extensive experience with many types of loops in C". I always thought there were only 4...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Quote Originally Posted by QuantumPete View Post
    Off-topic: This reminds me of a CV I saw a while back from someone who has "extensive experience with many types of loops in C". I always thought there were only 4...

    QuantumPete
    I don't mean I tried different kinds of loops I just mean different methods with loops

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quick question...how is your function, to take the two string arrays, determining thier array sizes? How are you declaring your array for the names and the e-mail address? If you can quickly give your code on what you have we may be able to better help.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Use strrchr(input, ' ') to find the last space, copy everything up to the last space to the name array, copy everything after to the email array.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Another suggesting if you are needing the name and e-mail to stick, per-se, to a unique record or list of employee contacts, I would recommend that you review how linked lists works, as that may be a better option if you are latter needing to add or remove within the listing, records, further down the road.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    methinks too you need linked lists.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    /****************************************************************************
     *                                                                          *
     * I doubt this is much help, but I thought I would write this for giggles. *
     *                                                                          *
     ****************************************************************************/
    
    /* Don't worry too much about what this structure is for. Just consider it magic */
    struct name_token_t
    {
      struct name_token_t *next, *prev;
      unsigned refs;
      char value[];
    };
    static name_token_t *tokpool = 0;
    
    
    /*
     * This is a tree that stores contact names. Why? Because a contact should be allowed more
     * than one email, correct?
     */
    struct name_t
    {
      struct name_t *left, *right;
      const char *first, *last;
    };
    
    /*
     * This is the heart of your initial question.
     */
    struct contact_info_t
    {
      struct contact_info_t *left, *right;
      struct name_t *name;
      const char *email;
    };
    
    /*
     *
    const char *tokdup(const char *s)
    {
      struct name_token_t *next, *prev, *tmp;
      size_t i;
    
      for(prev = 0, next = tokpool; next; next = next->next)
      {
        i = stricmp(s, next->value);
    
        if(!i)
        {
          ++next->refs;
          return next->value;
        }
    
        if(i < 0)
          break;
    
        prev = next;
      }
    
      /* Ok, if this point is reached, the token needs to be added */
      tmp = malloc(sizeof(*tmp) + strlen(s) + 1);
    
      if(tmp)
      {
        strcpy(tmp->value, s);
        tmp->prev = prev;
        tmp->next = next;
        tmp->refs = 1;
    
        if(!tokpool)
           tokpool = tmp;
      } else
      {
        fputs("*** Out of memory ***\n", stderr);
        return 0;
      }
    
      if(prev)
         prev->next = tmp;
    
      if(next)
         next->prev = tmp;
    
      return tmp->value;
    }
    
    void tokfree(const char *tok)
    {
      struct name_token_t *p = (struct name_token *)tok - 1;
    
      if(!tok)
        return;
    
      if(!--p->refs)
      {
        struct name_token *prev = p->prev, *next = p->next;
    
        if(prev)
           p->prev->next = next;
    
        if(next)
          p->next->prev = prev;
    
        if(p == tokpool)
          tokpool = next;
    
        free(p);
      }
    }
    
    void tokfree_all(void)
    {
      
    }
    
    /* Ok all that string memory management stuff can go on the back burner, I just put that 
     * to conserve duplicate strings. Its just a memory saver is all :)
     */
    struct name_t *make_name(const char *first, const char *last)
    {
      struct name_t *name = malloc(sizeof(*name));
    
      if(name)
      {
        name->first = tokdup(first);
        name->last = tokdup(last);
        name->right = name->left = 0;
      } else
        fputs("*** Out of memory ***\n", stderr);
    
      return name;
    }
    
    void remove_names(struct name_t *name)
    {
      if(name)
      {
        remove_names(name->left);
        remove_names(nams->right);
        tokfree(name->first);
        tokfree(name->last);
        free(name);
      }
    }
    
    
    /* I am sorting by first name, but you can change it to be by last if you wish. */
    struct name_t *update_names(struct name_t *root, const char *first, const char *last)
    {
      int cmp;
    
      if(!root)
        return make_name(first, last);
    
      cmp = strcmp(root->value, first);
    
      if(i == 0)
        return root;
      if(i < 0)
        root->left = update_names(root->left, first, last);
      if(i > 0)
        root->right = update_names(root->right, first, last);
    }
    
    struct contact_info_t *make_contact_info(struct name_t *name, const char *email)
    {
      struct contact_info_t *info = malloc(sizeof(*info));
    
      if(info)
      {
        info->name = name;
        info->email = tokdup(email);
      }
    
      return name;
    }
    
    /* Sorting this by email address, of course ;) */
    struct contact_info_t *update_contacts(constact_info_t *root, struct name_t *name, const char *email)
    {
      int cmp;
    
      if(!root)
        return make_contact_info(name, email);
    
      cmp = strcmp(root->email, email);
    
      if(i == 0)
        return root;
      if(i < 0)
        root->left = update_contacts(root->left, name, email);
      if(i > 0)
        root->right = update_contacts(root->right, name, email);
    }
    
    void remove_contacts(struct contact_info_t *info)
    {
      if(info)
      {
        remove_contacts(info->left);
        remove_contacts(info->right);
        tokfree(info->email);
        free(info);
      }
    }
    
    /* This should be enough to get someone started, imo. Now you have the ability to scan for contacts via
     * their name and email address. 
     */
    Perhaps from here you could write a simple "find" function.

    Example:
    Code:
    struct name_t *find(struct contact_info_t *root, const char *email)
    {
      if(root)
      {
        int cmp = strcmp(root->email, email);
    
        if(i < 0)
          return find(root->right, email);
        if(i > 0)
          return find(root->left, email);
        if(i == 0)
          return root->name;
      }
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Thanks for all of your help, it is working now

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Something tells me you are not using my code. Ironically, the most complicated part of my code is something that was entirely unnecessary to even put in the code (the string optimization code).

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by itCbitC View Post
    methinks too you need linked lists.
    Not necessarily. A dynamic array works, too.

    And I doubt arya6000 understands even half of that code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I always find it funny that the typical rationale for not using overly complex code is "the teacher will know I didn't write it." Never, "help me understand the code."

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Elysia View Post
    Not necessarily. A dynamic array works, too.
    Only if you simply want to store them but not if you want to search through the array; add to it or delete elements from it.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by QuantumPete View Post
    Off-topic: This reminds me of a CV I saw a while back from someone who has "extensive experience with many types of loops in C". I always thought there were only 4...

    QuantumPete
    More than that :-).

    • Recursion
    • goto
    • do while
    • while
    • for


    Probably even more.

  15. #15
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    I always find it funny that the typical rationale for not using overly complex code is "the teacher will know I didn't write it." Never, "help me understand the code."

    Actually, what about new users being a bit on the side, these guys wount have time to explain everything. One time, i was gona ask someone to explain a code i saw, but i bet i'd have gotten not even close to 3/4 of understanding. Dont know if it makes sense, thats my view. They know ppl wount explain everything, and for the level some of us are at then would just be more confusing i believe.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  2. 144KB email virus?
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 12-24-2004, 03:10 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. I got this in my email.
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 09:08 PM

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