user4833678
user4833678

Reputation:

The input is not working properly

The user has to input a number between 1 and 15 both inclusive. But my code accepts every single number. Even if I enter a negative number it does not call get the user input again. What should I change in the code? Thanks!

int enterNum(){

    printf("Enter a number between 1 and 15.\n");
    int input = 0;
    scanf(" %d", &input);

    while(input < 1 && input > 15){
        printf("Invalid input! Please try again!");
        scanf("%d", &input);
    }
    return input;
}

Upvotes: 2

Views: 42

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

input < 1 && input > 15 will never be true.

Use input < 1 || input > 15 instead.

Upvotes: 2

Related Questions