I have created a linked list, which prints the output just fine except only when the string name
contains a single digit 3
.I also tried fflush()
on stdin
and stdout
, without success.
The function:
void disp(node *head)
{
while(head->next!=NULL)
{
printf("\n%d\n%d\n",head->roll,head->marks);
puts(head->name);puts(head->add);
head=head->next;
}
printf("\nlast::::%d\n%d\n",head->roll,head->marks);
puts(head->name);puts(head->add);
}
Also, why there is a need of extra printing statements, after the while loop? Why the loop while(head->next!=NULL)
terminates on the second last node?
The code :
#include<stdio.h>
#include<conio.h>
typedef struct NODE
{
char *name,*add;
int roll,marks;
struct NODE *next;
}node;
void disp();
int main()
{
node *head,*temp,*cur; int ch;
head=(node *)malloc(sizeof(node));
printf("enter roll marks name add\n");
scanf("%d%d",&(head->roll),&(head->marks));
fflush(stdin);fflush(stdout);
gets(head->name);gets(head->add);
head->next=NULL;
temp=cur=head;
while(1)
{
printf("enter more?y/n\n");
ch=getche();
if(ch=='n')break;
temp=(node *)malloc(sizeof(node));
printf("enter roll marks name add\n");
scanf("%d%d",&(temp->roll),&(temp->marks));
fflush(stdin);fflush(stdout);
gets(temp->name);gets(temp->add);
temp->next=NULL;
cur->next=temp;
cur=temp;
}
disp(head);
getch();
return 66;
}
void disp(node *head)
{
while(head->next!=NULL)
{
printf("\n%d\n%d\n",head->roll,head->marks);
puts(head->name);puts(head->add);
head=head->next;
}
printf("\nlast::::%d\n%d\n",head->roll,head->marks);
puts(head->name);puts(head->add);
}
Sorry for an old compiler : Turboc (sorry its a compulsion)
UPDATE:
here is the output sample as requested :
enter roll marks name add
1
2
3
4
enter more?y/n
yenter roll marks name add
5
6
7
8
enter more?y/n
n
1
2
4
last::::5
6
7
8
As we can see, number 3 is not displayed.