Thread: Fill a linked list char by char

  1. #1
    Registered User w2look's Avatar
    Join Date
    Nov 2008
    Posts
    31

    Fill a linked list char by char

    Hi, I am writing a contact book program using linked lists and I want to fill the
    elements of the lists character by character. The reason I want to do it char by char
    is to allow spaces when I store strings for example when someone has a first name
    like Bobby Joe or when entering an address like 123 Any Street.

    Is this possible?

    I know how to add strings without spaces, but my attempt at char by char is
    resulting in Seg faults.

    Here is my attempt:

    Code:
    include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    struct contact					//structure declaration
    {
    	char fname[41];				//stores contacts first names
    	char lname[41];				//stores contacts last names
    	char address[101];			//stores contacts address
    	char pcode[7];				//stores contacts postal code
    	char phone[11];				//stores contacts phone number
    	struct contact *ptrNext;	//self-referential pointer
    };
    typedef struct contact Con;	//type declaration
    
    //declare global variables
    struct contact *ptrFirst = NULL;
    struct contact *ptrLast = NULL;
    
    //function prototypes
    struct contact *CreateCon();
    struct contact *InputCon(Con *ptrNew);
    void AddNew(Con *ptrNew);
    int isEmptyList();
    
    struct contact *InputCon(struct contact *ptrNew)
    {
    	char c;						//stores characters
    	int pos;					//array position variable
    	int spcount;				//space counter
    	int digcount;				//digit counter
    
    	fflush(stdin);				//flush the input buffer
    	printf("FIRSTNAME: ");		//prompt the user
    	pos = 0;					//reset counters
    	spcount = 0;
    	digcount = 0;
    	while( ( c = getchar() ) != '\n' )		//do until user hits enter
    	{
    		ptrNew->fname[pos] = c;
    		
    		if( isspace(&ptrNew->fname[pos]))
    		{
    			spcount++;				//if its a space, count it
    		}
    		if( isdigit(&ptrNew->fname[pos]))
    		{
    			digcount++;				//if its a digit count it
    		}
    		pos++;						//increment the position
    		//check the limit of the field
    		if(pos>39)
    		{
    			break;		//if you reach the limit break
    		}
    	}
    	ptrNew->fname[pos] = '\0';	//add string terminator
    .
    .
    ....roughly the same for last name, address, postal code and phone.
    }
    
    //Add new contact data interactively from the keyboard
    //Input: a self-referencing pointer to the new instance
    //Output: stores the data into the structure at that instance
    void AddNew(Con *ptrNew)
    {
    	if (isEmptyList())
    	{
    		ptrFirst = ptrNew;
    		ptrLast = ptrNew;
    	}
    	else
    	{
    		ptrNew->ptrNext = ptrFirst;
    		ptrFirst = ptrNew;
    	}
    }
    
    //check to see if the List is Empty
    //Input: none
    //Output: a boolean value for true or false
    int isEmptyList()
    {
    	if(ptrFirst == NULL)
    		return 1;
    	else 
    		return 0;
    }
    
    
    //function call in the program looks like...
    
    			case 1: // user typed 1 Add New Contact
    				
    				while((another == 'Y') || (another == 'y'))
    				{
    					another = 0;
    					AddNew(InputCon(CreateCon()));		//function call
    					num++;					//increment number of contacts
    					//let the user know the contact was added
    					printf("\nThe contact was added successfully.\n");
    					fflush(stdin);			//flush the input buffer
    					//prompt the user for input
    					printf("\nAdd another contact? (Yes = y, No = Any Key)\n");
    					another = getchar();		//get user input
    				}
    Any ideas how I can make this work? I would really appreciate some insight.
    I'm not looking for a fully coded solution, psuedocode that points me in the right
    direction would be great. I just don't know how to use the pointers right to
    correctly store the value.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by w2look View Post
    Hi, I am writing a contact book program using linked lists and I want to fill the
    elements of the lists character by character. The reason I want to do it char by char
    is to allow spaces when I store strings for example when someone has a first name
    like Bobby Joe or when entering an address like 123 Any Street.
    I'm going to hazard a guess here and say that this may be the tragic consequence of believing that there are only two commands for input in C (scanf, which is implicit in your fear of spaces, and getchar(), which you actually use).

    You can use fgets() with stdin; this will get you a complete line, spaces, punctuation, whatever you can type. It will include the newline.
    Code:
    char name[32];
    fgets(name,31,stdin);   /* leave one byte for null terminator */
    name[strlen(buffer)]='\0';  /* get rid of \n */
    If that is all you need to do, spare yourself this headache. Or have I misunderstood the goal?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User w2look's Avatar
    Join Date
    Nov 2008
    Posts
    31
    Thanks, that was actually my next question.
    I'll give gets a whirl and see if I can make it work.

  4. #4
    Registered User w2look's Avatar
    Join Date
    Nov 2008
    Posts
    31
    Thanks for the suggestion!

    I tried:

    Code:
    	fgets(ptrNew->fname,40,stdin) ;
    	ptrNew->fname[strlen(ptrNew->fname)]='\0';
    And it seems to work for as far as I have reached in the program thus far.
    If I run into further troubles, I'll post them here.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ptrNew->fname[strlen(ptrNew->fname)]='\0';
    this line is meaningless

    for any C-string
    str[strlen(str)] == 0

    so overwriting 0 with 0 has no point at all
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User w2look's Avatar
    Join Date
    Nov 2008
    Posts
    31
    Yeah, I have since figured that out.

    However, for my own purposes, I wanted to get rid of the \n being stored at the
    end of the string so I'm using it like:

    Code:
    ptrNew->fname[(strlen(ptrNew->fname))-1]='\0';
    and it seems to be doing what I want it to.
    If you know a reason why I should not do this, please post.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >If you know a reason why I should not do this, please post.
    If there is no '\n' there it will truncate the string by one.

    You have to find the '\n' and replace it if it is there, anything else is brittle. This is a FAQ.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread