How to limit user input to a certain number of digits?
I am writing a pretty simple program.
There are a few different inputs I need to get from the user.
1) Single character.
I am using getchar() for this, and it works well, but I've noticed one thing: If the user enters a number instead, the program will crash. How do I fix this?
2) Binary value
The user types in a binary value ie: 10101010
The problem is that I only want the user to be able to enter up to 8 bits. I am scanning it in as a string with scanf, then using strtol. As it is right now the user can enter more than 8 bits and the program will crash
3) Decimal value
Same problem as the binary value, I want to limit the user to inputting 3 digit decimal values.
4) Hex value
Same problem, I want to limit the user to entering 2 digit hex values.
So basically my problem is limiting user input. I'm sure this is a relatively simple problem, but I have been searching for an answer and my head is starting to spin.