Input Validation using Loops in C
In C, input validation ensures that user input meets the required constraints, such as valid numerical ranges or specific formats. We can use loops like while
and do-while
to repeatedly prompt the user until valid input is provided. This prevents incorrect values from causing unexpected behavior in a program.
Examples of Input Validation Using Loops
1. Validating a Positive Integer Input
In this example, we prompt the user to enter a positive integer. If the user enters a negative number or zero, we keep prompting until they enter a valid positive integer.
main.c
#include <stdio.h>
int main() {
int num;
// Loop to validate positive integer input
do {
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num <= 0) {
printf("Invalid input! Please enter a positive integer.\n");
}
} while (num <= 0);
printf("You entered: %d\n", num);
return 0;
}
Explanation:
- We declare an integer variable
num
to store the user input. - The
do-while
loop ensures the user is prompted at least once. - Inside the loop, we use
scanf("%d", &num)
to read an integer input. - If the input is less than or equal to 0, we display an error message.
- The loop continues until a valid positive integer is entered.
- Once a valid number is provided, we print it using
printf()
.
Output:
Enter a positive integer: -5
Invalid input! Please enter a positive integer.
Enter a positive integer: 0
Invalid input! Please enter a positive integer.
Enter a positive integer: 7
You entered: 7
2. Validating an Integer Within a Range
In this example, we ensure the user enters an integer within a specific range, say 1 to 100. If the input is outside this range, we prompt them again.
main.c
#include <stdio.h>
int main() {
int num;
// Loop to validate input within range 1 to 100
do {
printf("Enter a number between 1 and 100: ");
scanf("%d", &num);
if (num < 1 || num > 100) {
printf("Invalid input! Please enter a number between 1 and 100.\n");
}
} while (num < 1 || num > 100);
printf("Valid input received: %d\n", num);
return 0;
}
Explanation:
- We declare an integer variable
num
to store user input. - The
do-while
loop runs at least once to prompt the user. - We use
scanf("%d", &num)
to get an integer input. - If the number is not in the range 1 to 100, we display an error message.
- The loop continues until a valid number in the specified range is entered.
- Once a valid number is provided, we display it using
printf()
.
Output:
Enter a number between 1 and 100: 150
Invalid input! Please enter a number between 1 and 100.
Enter a number between 1 and 100: 0
Invalid input! Please enter a number between 1 and 100.
Enter a number between 1 and 100: 42
Valid input received: 42
3. Validating Character Input (Yes or No)
In this example, we validate character input to accept only Y
or N
(case insensitive). If the input is invalid, the user is prompted again.
main.c
#include <stdio.h>
#include <ctype.h>
int main() {
char choice;
// Loop to validate character input (Y/N)
do {
printf("Enter Y or N: ");
scanf(" %c", &choice);
choice = toupper(choice); // Convert to uppercase for case insensitivity
if (choice != 'Y' && choice != 'N') {
printf("Invalid input! Please enter Y or N.\n");
}
} while (choice != 'Y' && choice != 'N');
printf("You selected: %c\n", choice);
return 0;
}
Explanation:
- We declare a
char
variablechoice
to store user input. - The
do-while
loop ensures the prompt appears at least once. - We use
scanf(" %c", &choice)
to read a character input. - The function
toupper(choice)
converts lowercase letters to uppercase. - If the input is not
'Y'
or'N'
, an error message is displayed. - The loop repeats until a valid character is entered.
Output:
Enter Y or N: x
Invalid input! Please enter Y or N.
Enter Y or N: Y
You selected: Y
Conclusion
We explored different ways to implement input validation in C using loops:
- Ensuring a positive integer input.
- Validating input within a specific range.
- Restricting character input to specific values.
Using loops for input validation enhances the reliability of programs by preventing incorrect data entry.