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:
Any ideas how I can make this work? I would really appreciate some insight.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 }
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.



LinkBack URL
About LinkBacks



