My add method gets stuck after the user inputs a name. Can anyone help me fix it please.
Code:
void add(void) //FUNCTION TO ADD A RECORD IN LIST
{
int i,j,k;
char cho,ch;
k=1;
do
{
temp=(struct Record *)(malloc(sizeof(struct Record))); //DYNAMIC MEMORY ALLOCATION
printf("Enter Telephone Number : ");
scanf("%ld",&temp->telNo);
ch=getchar();
printf("Enter Name : ");
j=0;
ch=getchar();
while(ch!='n')
{
temp->name[j++]=ch;
ch=getchar();
}
temp->name[j]=' ';
printf(" Enter Record's Address:: ");
scanf("%s",temp->address);
printf("\n Enter Record's Year of Birth. :: ");
scanf("%d",&temp->yearOfBirth);
ch=getchar();
printf("\n Do You Want to Save This Record (y/n)??");
cho=getchar();
if((cho=='n')||(cho=='N'))
{
free(temp); //IF NOT TO SAVE THEN FREE THE ALLOCATED MEMORY
goto label;
}
temp->next=top;
top=temp;
printf(" One Record Added");
label:
ch=getchar();
printf(" Enter Another Record (y/n) ::");
cho=getchar();
}while((cho=='y')||(cho=='Y'));
}
Comment