0

How can I flush the input buffer without putting a newline character in scanf()? Because my proffessor doesn't like it. I tried fflush(); but it didn't work.

#include <stdio.h>
#include <conio.h>
int CountUpper(char S[],int n)
{
    int i,cntr = 0;
    for(i = 0; i < n; i++)
        if(S[i] >= 'A' && S[i] <= 'Z')
            ++cntr;
    return cntr;
}
int main(void)
{
    int n,i;
    printf("Enter n: ");
    scanf("%d",&n);
    char array[n];
    for(i = 0; i < n; i++)
    {
        scanf("%c",&array[i]);
        //fflush(stdin);
    }
    printf("Number of uppercase characters in array: %d\n",CountUpper(array,n));
    getch();
    return 0;
}
jantristanmilan
  • 4,188
  • 14
  • 53
  • 69
  • Possible duplicate of [Replacement of fflush(stdin)](http://stackoverflow.com/questions/6277370/replacement-of-fflushstdin), and literally hundreds of others that readily show up when searching for "scanf fflush". – DevSolar Sep 27 '12 at 14:01

1 Answers1

3

fflush is defined only for output streams and fflush(stdin) invokes undefined behaviour.

You can look into this to discard inputs in buffer: what can I use to flush input?

P.P
  • 117,907
  • 20
  • 175
  • 238