hi
I want some help
when I run the following code, it runs sucessfully for the first time when I add an account...........but when I try to add another it gives me an error "this program has performed an illegal operation & will be shut down"
when I ran the program for the first time it executed sucessfully.............it even added more than one record to linked list ............but when I tried to run it again it's giving me the above error
here's the code
/*understanding more about linked list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
void AddNewAccount(void);
void listall(void);
struct account
{
int number;
char name[25];
float balance;
struct account *link;
};
struct account *first,*current,*new1;
int anum=0;
int main()
{
char ch;
first=(struct account *)NULL;
current=(struct account *)NULL;
new1=(struct account *)NULL;
do
{
printf("A-add new account\n");
printf("L-list all the customers\n");
printf("Q-quit the program\n");
printf("\nEnter Your Choice : ");
ch=toupper(getch());
switch(ch)
{
case 'A':
puts("Add New Account : ");
AddNewAccount();
break;
case 'L':
listall();
case 'Q':
exit(0);
default:
break;
}
}
while(ch!='Q');
getche();
return 0;
}
void AddNewAccount(void)
{
char buffer[64];
new1=(struct account *)malloc(sizeof(struct account));
/*check to see if this is the fist record,if so initialize all the
pointers to this, first structure in the database*/
if(first==(struct account *)NULL)
first=current=new1;
else
{
/*otherwise find the end of the structure list
and add on the new structure you just allocated memory for*/
current=first; //making the first record the current one
while(current->link!=(struct account *)NULL) //loop through all records
current=current->link;
current->link=new1;
current=new1;
}
anum++;
printf("\n Account number : %5i\n",anum);
current->number=anum;
printf("\nEnter customer name : ");
gets(current->name);
printf("\nEnter customer balance : ");
current->balance=atof(gets(buffer));
current->link=NULL;
}
void listall(void)
{
if(first==(struct account *)NULL)
{
printf("\nThere's no record to be displayed");
}
else
{
current=first;
do
{
printf("\n%5i: %-15s $%8.2f",current->number,\
current->name,current->balance);
}
while((current=current->link)!=(struct account *)NULL);
}
}