Problem with creating linked list

This is a discussion on Problem with creating linked list within the C Programming forums, part of the General Programming Boards category; I am working on a program to create a simple customer database that stores customer names and their SSNs. The ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    Problem with creating linked list

    I am working on a program to create a simple customer database that stores customer names and their SSNs. The program should allow the user to add records, delete, print, and search.
    I just created the add customer (addcust) function. The program compiles without a problem but the problem is that when I execute the code and choose the Add Customer Option, and enter a customer name, the program prints "enter customer name" twice, before printing "Enter SSN".

    I guess you will only understand what I am saying if you execute this, so I have attached the code. Any help will be greatly appreciated. It probably is something small that I have overlooked but my brain is totally fryed right now so I cant think
    Thanks guys.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void addcust(void);
    void delcust(void);
    void printcust(void);
    void findcust(void);
    void searchcust(void);
    void savetofile(void);
    void nosave(void);
    
    struct customer {
    	char name[25];
    	char ssn[15];
    	struct customer * next;
    } ;
    
    int main(char ch)
    {
    
    	printf("\n\nCUSTOMER INFORMATION DATABASE\n\n");
    	printf("A - ADD Customer\n");
    	printf("D - DELETE Customer\n");
    	printf("P - PRINT Database\n");
    	printf("F - FIND Customer Name by SSN\n");
    	printf("S - SEARCH Customer SSN by Name\n");
    	printf("Q - QUIT Without Saving\n");
    	printf("E - SAVE and Exit\n\n");
    	printf("Enter a Letter, or E to quit without saving.\n");
    
    	while ((ch=getchar()) != 'E')
    	{
    		switch(ch)
    		{
    			case 'a' :
    			case 'A' : addcust();
    				       break;
    			case 'd' :
    			case 'D' : delcust();
    					   break;
    			case 'p' :
    			case 'P' : printcust();
    				       break;
    			case 'f' :
    			case 'F' : findcust();
    				       break;
    			case 's' :
    			case 'S' : searchcust();
    					   break;
    			case 'q' :
    			case 'Q' : savetofile();
    				       break;
    			case 'e' :
    			case 'E' : nosave();
    					   break;
    			default  : break;
    		}
    	}
    
    	return ch;
    	
    
    
    }
    
    
    void addcust(void)
    {
    	struct customer * head = NULL;
    	struct customer * prev, * current;
    	char customer[25];
    	
    
    	puts("Enter first customer: ");
    	while (gets(customer) != NULL && customer[0] != '\0')
    	{
    		current = (struct customer *) malloc(sizeof(struct customer));
    		if (head == NULL)
    			head = current;
    		else
    			prev->next=current;
    		current->next = NULL;
    		strcpy(current->name, customer);
    		puts("Enter SSN:");
    		scanf("%s", current->ssn);
    		while(getchar() != '\n')
    			continue;
    		puts("Enter next customer name (empty line to stop):");
    		prev=current;
    	}
    
    }
    void delcust(void){}
    void printcust(void){}
    void findcust(void){}
    void searchcust(void){}
    void savetofile(void){}
    void nosave(void){}

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    You have an unhappy mix of
    - getchar
    - scanf
    - fgets

    Replace them all with fgets() and appropriate actions on the buffer returned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    Well I picked up most of this code from a textbook and it was working fine there. I'm a newbie to C so you might have to clarify this. I havent implemented files yet so how can I use fgets instead of getchar or scanf? I'm still confused.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Things I noticed:
    1)
    Code:
    int main(char ch)
    See http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    Code:
    return ch;
    2)While nothing really wrong with that in of itself, you should be returning or EXIT_SUCCESS instead of 'E'
    3)You are using malloc but there is no free.
    4)Way to use fgets():
    Code:
    char buffer[20];
    fgets(buffer, sizeof(buffer), stdin)
    5)The link list won't operate correctly. You need to have the head pointer (at least) in the main routine and pass it to the functions. The way you have it right now the list head will be NULL everytime you invoke the add function
    Last edited by Thantos; 12-18-2003 at 03:35 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > int main(char ch)
    Should be
    int main(int argc, char *argv[])
    or
    int main ( void )

    > while ((ch=getchar()) != 'E')
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
        ch = buff[0];
    }
    > while (gets(customer) != NULL && customer[0] != '\0')
    No, don't use gets() - see the FAQ

    Use the same style of while loop shown previously (reading into a temp buffer), then strcpy the results to your malloc'ed structure.


    > current = (struct customer *) malloc(sizeof(struct customer));
    This is better written as
    current = malloc(sizeof(*current));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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