This question already has an answer here:
- scanf() leaves the new line char in the buffer 4 answers
here in this code i am creating a binary tree until user wants number of nodes. but while getting input from user it fails somewhere..
struct node *createTree(struct node *root)
{
int n;
char ch;
struct node *t1;
t1=(struct node *)malloc(sizeof(struct node));
printf("Enter Element\n");
scanf("%d",&n);
if(root==NULL)
{
t1->data=n;
t1->left=NULL;
t1->right=NULL;
root=t1;
}
printf("do you want to add node at left?\n");
this is not working properly
scanf("%c",&ch);
if(ch=='y')
{
t1->left=createTree(t1->left);
}
printf("do you want to add node at right?\n");
scanf("%c",&ch);
if(ch=='y')
{
t1->right=createTree(t1->right);
}
return root;
}