How do I go about Flushing STDIN here?

2019-09-12 03:22发布

I have a function (in C) that gets input from the user, (using scanf) stores it in an unsigned int, and returns the input to other functions that handle it:

unsigned
int input(void)
{
    unsigned int uin;

    scanf("%u", &uin);
    return val;
}

I was wondering, being as I ought to flush stdin, I'd want to use a while loop using getc, vis-a-vis:

while (getc != '\n') {
   ...
}

But, I'm not sure of how to:

A) Do operations inside of getc, as in how I ought to handle checks and concatenate the value, to each character gotten, or, whether or not I'm to getc, and then concatenate from there, removing scanf entirely.

B) Whether this is even the most proper way to do this.

Could some kind person give me some tips and pointers :)

Thanks.

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-12 03:37

I'd add %*[^\n] to the end of the scanf format string. The * means it should read but ignore the matching input, and the [^\n] means anything but a newline, just like in a regex.

查看更多
登录 后发表回答