jantristanmilan
jantristanmilan

Reputation: 4358

How do I flush the i/o stream? I'm using scanf(), fflush(stdin) doesnt work

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;
}

Upvotes: 0

Views: 7209

Answers (1)

P.P
P.P

Reputation: 121387

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?

Upvotes: 3

Related Questions