How do I go about Flushing STDIN here?

。_饼干妹妹 提交于 2019-12-12 05:14:10

问题


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:


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.



来源:https://stackoverflow.com/questions/1598131/how-do-i-go-about-flushing-stdin-here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!