Caffeinated
Caffeinated

Reputation: 12484

In C, How can I restrict the accepted values that this scanf will take?

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));

    }

Upvotes: 1

Views: 4822

Answers (5)

zmccord
zmccord

Reputation: 2612

(1) The check you're using is correct; .*scanf doesn't know integer range restrictions.

(2) As other posters noted, scanf() does not beget good user interfaces. Use readline() to get lines and sscanf to parse them and you'll be happier.

(3) You're restricting these values for security purposes? If this is actually a security issue, those should not be magic numbers but #defines or static consts or globals, so that if somebody changes those ranges without updating your code your security check isn't compromised.

Upvotes: 2

leocod
leocod

Reputation: 173

This code should restrict the value the patient_id should have. This is not the most efficient way by far, but it if you want to use scanf(), then this can do the job.

#include <stdio.h>
int patient_id(void);

int main(void)
{
 int id=0;
 id = patient_id();

 printf("\nThe patient_id is: %d\n", id);
}

int patient_id(void)
{
    int p_id;
    int ch;

    printf("Enter ID between 10000 & 99999: ");
    scanf("%d", &p_id);

    while ((p_id <10000 || p_id >99999))
        {
            while((ch = getchar()) !='\n')
               continue;

            printf("Please enter a value between 10000 and 99999:  ");
            scanf("%d", &p_id);
        }
    return p_id;
}

Upvotes: 2

Sachin Mhetre
Sachin Mhetre

Reputation: 4543

You can not contol input values while accepting these values. You have to first accept values and then you can check condition whether the input accepted is correct or not. You can generate a warning for wrong inputs.

In above code if you want patient_id between 10000 to 99999, then your code is correct, but user will enter values till he/she enters a correct input.

eg. 1, 200124, 45, 9999, 321, 12001. Here your prgm will stop accepting input as user enters 12001.

Upvotes: 1

Julian Fondren
Julian Fondren

Reputation: 5619

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.

Upvotes: 4

Nikson Kanti Paul
Nikson Kanti Paul

Reputation: 3440

your condition should be

while((patient_id < 10000) || (patient_id > 99999))

for this purpose

Upvotes: 2

Related Questions