clsInputValidate
is a powerful and reusable C++ class that simplifies and secures user input by validating different data types before accepting them. It ensures your program doesnβt crash from unexpected or invalid input.
When building console applications, users can enter incorrect data (like letters when numbers are expected). This class prevents such issues by:
- Forcing valid input types
- Ensuring numbers or dates fall within acceptable ranges
- Providing clear prompts and error messages
- Making the code cleaner and more maintainable
Feature | Description |
---|---|
Integer & Double Reading | Forces correct numeric input |
Range Checking | Ensures number is within a valid range |
Character Input | Accepts only a single character |
Yes/No Prompts | Accepts only 'Y' or 'N' (case-insensitive) |
Date Validation & Range | Validates and compares custom clsDate objects |
#include <iostream>
#include <string>
#include <limits>
#include "clsDate.h"
#include "clsString.h"
You must define clsDate
and clsString
in your project for this to work correctly.
Check whether a number is between two values (inclusive):
bool isNumberBetween(int value, int from, int to);
bool isNumberBetween(double value, double from, double to);
π Example:
if (clsInputValidate::isNumberBetween(5, 1, 10)) {
cout << "Number is valid!";
}
Check if a date is between two other dates using your custom clsDate
type.
bool isDateBetween(clsDate dateToCheck, clsDate date1, clsDate date2);
Read an integer safely with a custom prompt and warning:
int readNumberInteger(string prompt, string warning);
Read integer between two values:
int readNumberIntegerBetween(int from, int to, string prompt, string warning);
π§ͺ Example:
int age = clsInputValidate::readNumberIntegerBetween(18, 99, "Enter your age: ", "Age must be between 18 and 99!
");
double readNumberDouble(string prompt, string warning);
double readNumberDoubleBetween(double from, double to, string prompt, string warning);
π§ͺ Example:
double price = clsInputValidate::readNumberDouble("Enter price: ", "Invalid number!
");
Reads only a single character input:
char readCharacter(string prompt = "", string warning = "Please enter a single character:");
Reads only Y or N, case-insensitive:
char answerYesOrNo(string prompt = "", string warning = "Please enter Y or N only:");
π§ͺ Example:
char response = clsInputValidate::answerYesOrNo("Continue? [Y/N]: ");
Delegates to your clsDate::validationDate()
function:
bool validationDate(clsDate date);
Enter your age: ahmed
Invalid input. Try again: 25
Enter price: five
Invalid number! Please try again: 5.75
Continue? [Y/N]: maybe
Please enter Y or N only: y
This shows how the class guards the program from bad input.
- Ideal for menus, banking systems, admin panels, etc.
- Replace
cin >> var;
with these functions for safe input - You can customize all messages for localization or branding
Love this project? Want to improve it?
Feel free to submit Issues or Pull Requests on GitHub β your contributions are highly appreciated! π
Ahmed Jehad Ahmed
π GitHub Profile
π§ Email Contact