开发者

How do I go about Flushing STDIN here?

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 a开发者_StackOverflows 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜