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