CSETanjil's blog

By CSETanjil, history, 5 years ago, In English

When I want to get multiple input on c program I was a faces a common problem. That was , If I want to place to scanf() functions then I observer that one of function doesn't work at the right time. Like one of the scanf() function miss out from the compiler or program controller. First of I couldn't not find out the original fact to occur this type of problem. After finding some cases I observed that , when The first scanf() function evaluated that execution and second of the function are keeping first function enter keyword. Because the buffer memory keep the value of the enter key and store this value of the second function. To solve this problem of getting input from keyboard on c program you should use another extra function that is define the header file stdio.h . the format of this code is:

int main() { int x[100],i,size; scanf("%d",&x[i]); fflush(stdin); scanf("%d",&size); fflush(stdin); for(i=0;i<size;i++) scanf("%d",&x[i]); return 0; }

The fflush function clear the buffer which previously store some unnecessary values and You get a real value which you are want.

  • Vote: I like it
  • -11
  • Vote: I do not like it

| Write comment?