Thread: coding problem

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

    coding problem

    hi,
    im trying to write a prog for a linked list. the code is shown below. it basically refuses to run. hwen i compile it i get no errors, but im pretty sure one of my pointers is referancing a bogus address, there fore its not running past the first entry.
    the function of the list is to input data of members into the linked list and tehn print it to screen. there is a switch-case menu there as well.
    please help me solve this problem. its doing my head in!!!

    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_rec;
    };
      _members *new_rec, *head, *current_rec;
    
    int mainmenu (_members *current_rec, _members *new_rec, _members *head);//prototype
    void AddEntry     ( _members *head, _members *new_rec, _members *current_rec);
    void PrintEntries ( _members *head, _members *current_rec);
    void AddEntry (_members *head, _members *new_rec, _members *current_rec)
    {
    	  current_rec = head;
    	  while (current_rec -> next_rec != NULL)
    	  {
    		current_rec -> next_rec;
    	  }
    	  new_rec = (_members*)malloc(sizeof(struct _members));
    	  current_rec -> next_rec = new_rec;
    	  new_rec -> next_rec = 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(current_rec,new_rec,head);
    
    }
    
    
    void PrintEntries(_members *head, _members *current)
    {
    	current_rec = head;
    	while (current_rec != NULL)
    	{
     printf ("Membership ID: %d\n ",    current_rec -> mem_id);
     printf ("\nStatus     : %s\n",     current_rec -> mem_status);
     printf ("\nName       : %s  %s\n", current_rec -> name, current_rec -> surname);
     printf ("\nAddress    : %s\n%s\n", current_rec -> flatNo, current_rec -> Street);
     printf ("\nPost Code  : %s\n    ", current_rec -> pst_cde);
     printf ("\nTelephone  : %s\n",     current_rec -> Phone);
    	}
    	current_rec = current_rec -> next_rec;
         mainmenu(current_rec,new_rec,head);
    
    }
    
    
    int mainmenu(_members *current_rec, _members *new_rec, _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,current_rec,head);
    				break;
    		case 2:
    				PrintEntries(current_rec,head);
    				break;
    		case 3:
    				return 0;
    
    		default:
    			puts("INCORRECT ENTRY. PLEASE TRY AGAIN\n\n");
    		}
    	}
    
    	return 1;
    }
    
    int main(void)
    {
        _members *new_rec, *current_rec,*head;
    
    	head = NULL;
    	new_rec = NULL;
    	current_rec = NULL;
    
    	new_rec = (_members*)malloc(sizeof(struct _members));
    	new_rec -> next_rec = head;
    	head = new_rec;
       mainmenu(current_rec,new_rec,head);
       return 0;
    }
    hope i used the code tages properly.... sorry if i dnt, im new so any help with the forum rules will be much appreciated

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First thing to clarify is which language are you writing in? C or C++?

    Follow the variables through your code. For example, you call AddEntry like so:

    >>AddEntry(new_rec,current_rec,head);

    but AddEntry is defined like this:

    >>void AddEntry (_members *head, _members *new_rec, _members *current_rec)

    Note the variable name order doesn't appear to match. It will compile and run, but it may not give results you are expecting.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

    hiya,

    i jus tried all th stuf fu said, but it still not running properl, please help. im coding in c and im using microsoft visual c++ as the compiler (university software).
    thanx

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: hiya,

    Originally posted by mandiamos
    i jus tried all th stuf fu said, but it still not running properl, please help. im coding in c and im using microsoft visual c++ as the compiler (university software).
    thanx
    I suggest signing up for English 101, because no one here knows what the hell you're trying to say.

    On a related note, post your latest code and what errors you get, as well as suggested input, and the output you get, assuming it compiles at all.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 16
    Last Post: 01-26-2006, 02:38 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM