Hi,
I'm doing a little nasmx86 program, and at many places I need to get user input, with an specified length.
I'm using Linux and building as:
nasm -f elf32 -o
gcc -m32 -o
I would like to know if the user entered more/less than the expected, in which case I would like to clear stdin, and tell the user to try again.
gets:
The problem with this, is I can't control how much input the user will enter, I'm using gets, and then copying "n" bytes to another string, so if I want a 5 char name, and the user enters "123456", it won't cause any problem, because, gets has a large reserved memory to allocate it, then I copy the first 5 bytes to another place and it's done. But it doesn't look good visually.
scanf:
Using special arguments ("%3s") solves the problem, but if the user entered more than the allowed digits, those digits will stay in stdin, and will be used when there's a new call that uses stdin.
fgets:
Same problem with scanf...
I've tried...
- For the case of fgets, and scanf, if the user entered more than the allowed digits I can do a dummy call to gets, to flush stdin, but if the user entered less than the allowed digits, the dummy call to gets is useless, and will make the program ask for non required additional input.