How do I go to Flushing STDIN?

I have a function (in C) that receives input from a user (using scanf) saves it in an unsigned int and returns input to other functions that process it:

unsigned int input(void) { unsigned int uin; scanf("%u", &uin); return val; } 

I was wondering that since I have to clear stdin, I would like to use a while loop using getc, vis-a-vis:

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

But I'm not sure how:

A) Perform operations inside getc, as in how I should process the checks and concatenate the value, to each character received, or regardless of whether I should getc and then concatenate from there, completely removing scanf.

B) This is even the most correct way to do this.

Can any person give me some tips and pointers :)

Thanks.

0
source share
1 answer

I would add %*[^\n] to the end of the scanf format string. * means that it should read, but ignore the corresponding input, and [^\n] means anything other than a newline, as in a regular expression.

+4
source

Source: https://habr.com/ru/post/1310882/


All Articles