Thread: Need help with code

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    Question Need help with code

    Hi,
    I've written this function for a phone directory and it is essential that it is good programming code..but for some reason its not really working..the names are not being stored where they should be and thats frustrating me..please help..Thanks
    Please ignore the deleteNode function..but look at the rest..thanks
    A
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct
    { char names[20];
    } KEY;
    
    
    typedef struct
    { 
    	KEY k;
    	char phone[10];
    } DATA;
    
    typedef struct nodetag
    {
    	DATA directory;
    	struct nodetag *next;
    } NODE;
    
    
    int getOption();
    
    void options();
    
    
    void errorMsg();
    
    char anotherContact();
    
    void createList(NODE *pList,
    				NODE *pPre,
    				NODE *pNew,
    				DATA directory);
    
    int searchList(NODE *pList,
    			   NODE **pCur,
    			   NODE **pPre,
    			   KEY k);
    
    
    int cmp(KEY k, KEY k2);
    
    NODE *insertNode(NODE *pList,
    				 NODE *pPre,
    				 DATA directory);
    
    
    
    NODE *deleteNode(NODE *pList,
    				 NODE *pPre,
    				 NODE *pCur);
    
    DATA getDetails();
    void printNode(NODE *pCur);
    
    void printList(NODE *pList);
    
    int main(void)
    {
    	int num = 1;
    	DATA directory;
    	NODE *pList;
    	NODE *pPre;
    	NODE *pNew;
    	NODE *pCur;
    	int temp;
    	KEY k;
    
    
    	options();
    
    	num = getOption();
    
    	pPre = pList = pNew = pCur = NULL;
    	
    	while(num)
    	{
    		switch(num)
    		{
    		case 1: createList(pList, pPre, pNew, directory);
    				break;
    
    		case 2:	printf("Enter the first name of contact you wish to search: ");
    				scanf("%s", &k.names);
    				temp = searchList(pList, &pCur, &pPre, k);
    				if(temp == 1)
    				{
    					printf("Directory listing:\t\n");
    					printf("NAME\t\t\tPHONE #\n");
    					printNode(pCur);
    				}
    				else
    				{
    					errorMsg();
    				}
    				break;
    
    		case 3:	directory = getDetails();
    				pList = insertNode(pList, pPre, directory);
    				break;
    
    		case 4: deleteNode(pList, pPre, pCur);
    				break;
    
    		case 5: printList(pList);
    				break;
    
    		case 6: printf("\nBye Bye Now!!\n\n");
    				return 0;
    		}
    	
    		printf("\nWhat would you like to do next: ");
    		num = getOption();
    
    	}
    	return 0;
    
    }
    
    
    /**************************getOption***********************
    */
    int getOption()
    {
    	int x; 
    	scanf("%d", &x);
    	while(x <= 0 || x > 6)
    	{
    		printf("Invalid Input\n");
    		printf("Please enter a number from the menu above: ");
    		scanf("%d", &x);
    	}
    	return x;
    }
    
    /********************options*************************************
    */
    
    void options()
    {
    	printf("What would you like to do today?\n");
    	printf("1. Create a phone list\n");
    	printf("2. Search for a phone number by name\n");
    	printf("3. Add a new name and phone number to the list\n");
    	printf("4. Delete a name from the list\n");
    	printf("5. Print the list\n");
    	printf("6. Quit\n");
    	printf("Choose an option from the menu above: ");
    	return;
    }
    
    /************************errorMsg*******************************
    */
    void errorMsg()
    {
    	printf("ERROR: Name not found!! Please try again");
    }
    
    /**************************anotherContact***********************
    */
    
    char anotherContact()
    {
    	char cont;
    
    	printf("\nWould You Like To Add Another Contact (y/n)?");
    	scanf("%c", &cont);
    
    	return cont;
    }
    /***********************createList*******************************
    */
    
    void createList(NODE *pList,
    				NODE *pPre,
    				NODE *pNew,
    				DATA directory)
    {
    
    	char cont = 'y';
    	int locn;
    	NODE *pCur;
    
    	pList = NULL;
    	
    	while(cont == 'y' || cont == 'Y')
    	{
    			directory = getDetails();		
    			locn = searchList(pList, &pCur, &pPre, directory.k);
    			if(locn == 0)
    			{
    				pList = insertNode(pList, pPre, directory);
    				cont = anotherContact();
    			}
    			else
    			{
    				printf("\a\nName already exists in directory!!!!\n");
    				cont = anotherContact();
    			}	
    	
    	}
    }
    
    /*************************searchList*****************************
    */
    
    int searchList(NODE *pList, 
    			   NODE **pCur,
    			   NODE **pPre,
    			   KEY k)
    
    {
    	int found = 0;
    	// initialize
    	*pPre = NULL;
    	*pCur = pList;
    
    	//Start search
    	while(*pCur != NULL && (cmp(k, (*pCur)->directory.k) > 0))
    	{
    		*pPre = *pCur;
    		*pCur = (*pCur)-> next;
    	}
    
    	if(*pCur && (cmp(k, (*pCur)->directory.k) == 0))
    	{
    		found = 1;
    	}
    
    	return found; 
    }
    
    
    /*******************compare**************************************
    */
    
    int cmp(KEY k, KEY k2)
    {
    	int result;
    	int cmp;
    	cmp = strcmp(k.names, k2.names);
    	if(cmp > 0) 
    	{
    		result = 1;
    	}
    	else if(cmp == 0) 
    	{
    		result = 0;
    	}
    	else 
    	{
    		result = -1;
    	}
    
    	return result; 
    }
    
    /************************insertNode******************************
    
    */
    NODE *insertNode(NODE *pList,
    				 NODE *pPre,
    				 DATA directory)
    {
    	NODE *pNew;
    	DATA d;
    
    	if(!(pNew = (NODE*)malloc(sizeof(NODE))))
    	printf("\aMemory Overflow in insert\n"), exit (100);
    
    	pNew->next = NULL;
    	if(pPre == NULL)
    	{
    		pNew->next = pList;
    		pList = pNew;
    	}
    	else
    	{
    		pNew->next = pPre->next;
    		pPre->next = pNew;
    	}
    	return pList;
    }
    
    
    /************************deleteNode******************************
    */
    
    NODE *deleteNode(NODE *pList,
    				 NODE *pPre,
    				 NODE *pCur)
    {
    	if(pPre == NULL)
    		pList = pCur ->next;
    	else
    		pPre->next = pCur->next;
    
    	free(pCur);
    	return pList;
    }
    
    /*********************getDetails*********************************
    */
    
    DATA getDetails()
    
    {
    	DATA d;
    
    	fflush(stdin);
    	printf("\nPlease enter name of contact you wish to add:");
    	gets(d.k.names);
    	printf("Please enter phone number:");
    	gets(d.phone);
    
    	return d;
    }
    /**********************printNode**********************************
    
    */
    void printNode(NODE *pCur)
    {
    
    	printf("%-20s\t",pCur->directory.k.names);
    	printf("%10s", pCur->directory.phone);
    	printf("\n");
    
    	return;
    }
    
    
    
    /************************printList***********************************
    */
    
    void printList(NODE *pList)
    {
    	NODE *pWalker;
    
    	pWalker = pList;
    	
    	if(pWalker == NULL)
    		printf("\n\aWARNING: List Currently Empty!!!\n ");
    	else
    	{
    		printf("\n\nPhone Directory contains:\n\n");
    		printf("\nNAME\t\tPHONE #\n");
    			while(pWalker)
    			{
    				printNode(pWalker);
    				pWalker = pWalker->next;
    			}
    		printf("\n");
    	}
    
    	return;
    }
    Last edited by aspand; 06-12-2002 at 11:48 PM.

  2. #2
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    why don't you use a single struct for the names and phone numbers?

    [/code] works better.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    cos we're supposed to used two structures..considered more efficient and good programming..
    my DATA directory is not getting filled with the information I input..can somebody try it and see whats going wrong..it has 0 errors 1 stupid warning..but its not working and its ........ing me off!!!
    A

  4. #4
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    If you need to use two structures, that is fine, but why don't you put the DATA and KEY structure in the NODE structure, instead of putting the KEY in the DATA.

    make your code tag right so people can read it.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    thats what the prof wants. I know the problem lies in the createList and SearchList functions esp with passing parameters..could you have a look at that? I know the structure definition is just fine..
    Thanks tho
    A

  6. #6
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    you need to edit your first post, and fix the code tag. [\code] to [/code].
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    sorry..I guess I didn't read it the last two times you mentioned it..hope you can help now

  8. #8
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    you can't create a list without a list. You should create another structure for the list.

    typedef struct list {
    int count;
    int position;
    NODE *head;
    } List;

    Then to create a list all you will want to do something like this.

    void CreateList (List *list)
    {
    list->head = NULL;
    list->count = 0;
    list->position = -1;
    }
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    can you just have 2 structures, then when you traverse through the list for example in your search, dont pass the pointers memory address, just pass the value of the pointer (address of the first item). ie

    Code:
    typedef struct
    {
      char names[20];
    } KEY;
    
    typedef struct data
    { 
      KEY k;
      char phone[10];
      struct data *next;
    } DATA;
    
    
    
    int main()
    {
      DATA tempdata;
      DATA *phonebook=NULL;
      char name[20];
    
      printf("Enter search name: ");
      scanf("%s",name);
      tempdata=searchlist(phonebook,name,&tempdata);
    }
    
    searchlist(DATA *phonebook, char name[], DATA *tempdata)
    {
      while(phonebook!=NULL && strcmp(phonebook->k.names,name)<>0)
        phonebook=phonebook->next;
      if(phonebook!=NULL)
      {
        strcpy(tempdata->k.names,phonebook->k.names);
        strcpy(tempdata->phone,phonebook->phone);
      }
    }
    something like that is the way I'd go about it, upto u.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    71
    need more help..whats not right with my createList and SearchList functions?? why are they not taking in data??
    A

  11. #11
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    did you just try to code this straight thru without testing each function as you went.

    for starters:
    >directory = getDetails();

    you can't do that. You need to pass a pointer to the data structure instead.

    void getDetails(Data *directory);
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    I know..but doesn't getDetails return a structure anyways?? shouldn't that be fine?
    need more..more..more help
    A

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    Does anyone know how to fix this lab??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM