Skip to content

7mee3d/InputValidator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 

Repository files navigation

InputValidator πŸ“₯

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.


πŸš€ Why Use InputValidator?

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

πŸ”§ Features

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

πŸ“¦ Required Libraries

#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.


πŸ“˜ Functions and Usage

1. βœ… Number Between Check

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!";
}

2. πŸ“† Date Between Check

Check if a date is between two other dates using your custom clsDate type.

bool isDateBetween(clsDate dateToCheck, clsDate date1, clsDate date2);

3. πŸ”’ Read Integer

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

4. πŸ“ˆ Read Double

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

5. πŸ”  Read Character

Reads only a single character input:

char readCharacter(string prompt = "", string warning = "Please enter a single character:");

6. βœ… Yes or No Prompt

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]: ");

7. πŸ“… Validate Date

Delegates to your clsDate::validationDate() function:

bool validationDate(clsDate date);

πŸ§ͺ Sample Interactive Session

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.


🧠 Tips for Integration

  • 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

🀝 Contributing

Love this project? Want to improve it?
Feel free to submit Issues or Pull Requests on GitHub β€” your contributions are highly appreciated! πŸ™Œ


πŸ‘¨πŸ’» Author

Ahmed Jehad Ahmed

πŸ”— GitHub Profile

πŸ“§ Email Contact


About

πŸ“š InputValidator is a simple C++ library that helps safely read and validate user input like numbers, dates, and characters. Great for preventing input errors and improving user experience in console applications.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages