help with linked list

This is a discussion on help with linked list within the C Programming forums, part of the General Programming Boards category; hi, i have the code below, and i am unable to get it to run. on compilation i get no ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    3

    help with linked list

    hi,
    i have the code below, and i am unable to get it to run. on compilation i get no errors, but when i try to run the program i only get the first field of the list (the membersID request) please help, i cant seem to find the problem.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    const int length = 50;
    const int type  = 10;
    
     struct _members
    {
      int mem_id;
      char mem_status [type];
      char name       [length];
      char surname    [length];
      char flatNo     [length];
      char Street     [length];
      char pst_cde    [type];
      char Phone      [length];
      _members *next;
    };
      _members *new_rec, *head, *curr;
    
    int mainmenu      ( _members *new_rec, _members *curr,  _members *head);//prototype
    void PrintEntries ( _members *curr, _members *head);
    void AddEntry     ( _members *new_rec, _members *curr, _members *head);
    
    
    void AddEntry     ( _members *new_rec, _members *curr, _members *head)
    {
    	  
    
    	curr = head;
    	  while (curr -> next != NULL)
    	  {
    		curr -> next;
    	  }
    	  new_rec = (_members*)malloc(sizeof(struct _members));
    	  curr -> next = new_rec;
    	  new_rec -> next = NULL;
    
    printf ("\nEnter Member ID         : "); scanf ("%d", new_rec -> mem_id); 
    printf ("\nEnter Member status     : "); scanf ("%s", new_rec -> mem_status);
    printf ("\nEnter Name              : "); scanf ("%s", new_rec -> name);
    printf ("\nEnter Surname           : "); scanf ("%s", new_rec -> surname);
    printf ("\nEnter flat/house Number : "); scanf ("%s", new_rec -> flatNo);
    printf ("\nEnter Street Name       : "); scanf ("%s", new_rec -> Street);
    printf ("\nEnter Post Code         : "); scanf ("%s", new_rec -> pst_cde);
    printf ("\nEnter Contact Number    : "); scanf ("%s", new_rec -> Phone);
    
    	  
    	 mainmenu(new_rec, curr, head);
    
    }
    
    
    void PrintEntries(_members *curr, _members *head )
    {
    	curr = head;
    	while (curr != NULL)
    	{
     printf ("Membership ID: %d\n ",    curr -> mem_id);
     printf ("\nStatus     : %s\n",     curr -> mem_status);
     printf ("\nName       : %s  %s\n", curr -> name, curr -> surname);
     printf ("\nAddress    : %s\n%s\n", curr -> flatNo, curr -> Street);
     printf ("\nPost Code  : %s\n    ", curr -> pst_cde);
     printf ("\nTelephone  : %s\n",     curr -> Phone);
    	}
    	curr = curr -> next;
    
         mainmenu(new_rec, curr, head);
    
    }
    
    
    int mainmenu( _members *new_rec, _members *curr, _members *head)
    {
    	int reply = 0;
    
    	puts("MAIN MENU\n");
    	puts(" 1.Add an Entry.");	
    	puts(" 2.Print All Entries.");
    	puts(" 3.Quit.");
    	printf("\n\nChoose one: "); scanf ("%d", & reply);
    
    	while (reply != 3)
    	{
    		switch (reply) 
    		{
    		case 1:
    				AddEntry(new_rec, curr, head);
    				break;
    		case 2:
    				PrintEntries(curr, head);
    				break;
    		case 3:
    				return 0;
    
    		default:
    			puts("INCORRECT ENTRY. PLEASE TRY AGAIN\n\n");
    		}
    	}
    
    	return 1;
    }
    
    int main(void)
    {
        _members *new_rec, *curr,*head;
    
    	head = NULL;
    	new_rec = NULL;
    	curr = NULL;
    
    	new_rec = (_members*)malloc(sizeof(struct _members));
    	new_rec -> next = head;
    	head = new_rec;
       mainmenu(new_rec, curr,head);
       return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you're compiling this as C++, and not C. If you weren't, it wouldn't compile. In C, unless they changed it in the C99 standard, and you're using a C99 compiler, you cannot do this:
    Code:
    struct _members
    {
      int mem_id;
      char mem_status [type];
      char name       [length];
      char surname    [length];
      char flatNo     [length];
      char Street     [length];
      char pst_cde    [type];
      char Phone      [length];
      _members *next;
    };
      _members *new_rec, *head, *curr;
    
    int mainmenu      ( _members *new_rec, _members *curr,  _members *head);//prototype
    None of the bolded section is legal in C. You must either typedef this to its own type, or you must provide the keyword struct before the word _members.

    Next, you're typecasting the return value of malloc, which again tells me you're compiling it as C++. In C there is no need to typecast the return of malloc. This I believe is in the FAQ. If not, search the forum, you'll find ample reference on it.

    From there... I'm going to crash for the day, so that'll have to do to get you started.

    Quzah.
    Hope is the first step on the road to disappointment.

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. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  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