0

In Windows i used flushall() function to flush all the buffers but this doesnt work in Linux, my scanf() function skips without scanning:

for(i=0;i<n;i++)
   {
    printf("\nEnter alphabet :");
    scanf("%c",&x);
    printf("\nEnter frequency :");
    scanf("%f",&probability);
  /* create a new tree and insert it in
     the priority linked list */
    p=(treenode*)malloc(sizeof(treenode));
    p->left=p->right=NULL;
    p->data=x;
    p->freq=(float)probability;
    head=insert(head,p);
  }

Output :

mayur@mayur-laptop:~$ ./a.out

Enter alphabet :a

Enter frequency :2

Enter alphabet :
Enter frequency :a

Enter alphabet :
Enter frequency :2

Enter alphabet :
Enter frequency :a

Enter alphabet :
Mayur Kulkarni
  • 1,306
  • 10
  • 28
  • possible duplicate of [Scanf skips every other while loop in C](http://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c) – axiac Apr 20 '15 at 21:20
  • In its current form (using `scanf("%c")` the question is a duplicate of many other questions about this behaviour of `scanf()`. – axiac Apr 20 '15 at 21:22

2 Answers2

0

Update: The OP changed the "%d"in the first scanf to "%c" which lets the error, I think, occur later; but franky, I don't feel investing any more time here.--

Original answer: The input is never processed beyond the 'a' because you try reading it with the conversion specification %d for integer numbers which it doesn't satisfy. (In order to read a char you would specify %c.) Scanf puts the offending character back in the input and tries to read the next number which fails again, and so on ad eternum.

It's worth checking scanf's return value which will always be 0 here, indicating that no successful conversion has taken place.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • sorry , I wrote the code in hurry , the output i mentioned is for character 'x' , would you correct my code? – Mayur Kulkarni Apr 20 '15 at 20:07
  • Quite a chuzpe. Why don't you put a maximum effort into reading the relevant documentation, writing your code, testing and debugging it? Usually that resolves problems. If you then still have questions, provide a minimal self-contained example, run it with maximum attention to correct input and documentation, and ask again. Instead of wasting my time. – Peter - Reinstate Monica Apr 20 '15 at 20:20
0

You should add a space on the beginning of the scanf, and fflush(stdin) before every scanf just to clear the standard input buffer (keyboard by default), like this:

for(i=0;i<n;i++){
    printf("\nEnter alphabet :");
    fflush(stdin);
    scanf(" %c",&x);
    printf("\nEnter frequency :");
    fflush(stdin);
    scanf(" %f",&probability);
  /* create a new tree and insert it in
     the priority linked list */
    p=(treenode*)malloc(sizeof(treenode));
    p->left=p->right=NULL;
    p->data=x;
    p->freq=(float)probability;
    head=insert(head,p);
  }

EDIT: check if you have char x and float probability

user24100
  • 27
  • 1
  • 6
  • 1) `fflush(stdin)` is not well defined on many platforms. Better to avoid. Curious: what reference/person suggested using `fflush(stdin)`? 2) The space in `scanf(" %f",&probability);` is not needed as `"%f"` consumes leading white-space already, though does not cause much harm – chux - Reinstate Monica Apr 20 '15 at 22:03
  • I just recommend based on previous programs I had to do. I share your opinion and I dislike fflush, but just in case... – user24100 Apr 20 '15 at 22:06