I’d like to restrict the possible values that the following patient_id variable can have(for security reasons). The following is the code I currently have, but I’m pretty sure this is the wrong approach:
void addPatient(){
int patient_id;
printf("Enter ID between 10000 & 99999: ");
do{
scanf("%d", &patient_id);
}while((patient_id<10000)&&(patient_id>99999));
}
Unless you’re writing a program for homework problem, and in particular if you’re writing a program that an actual human user will interact with in any serious way, don’t use scanf(). Operate on lines of input at a time. If you still want to use scanf()’s ‘parsing’, you can use sscanf() and like on a complete line of input after you have it. If you don’t know how to get user input other than through scanf(), a simple way is to fgets() into a buffer, pick out the line, and prepare the buffer for the next fgets().
If you insist on using scanf(), I have a challenge for you: use it to accept two IDs on the same line, separated by a space. If the user enters only one ID and then hits enter, your program should complain about this to the user before requesting any more input.