https://github.com/mpluxery/basic-cpp-age-checker
Simple C++ console application that prompts the user to enter their age, validates that the input is a number between 1 and 99; otherwise, it displays an error message and re-prompts until a valid age is entered.
https://github.com/mpluxery/basic-cpp-age-checker
beginner-code beginner-friendly c-plus-plus cpp else-if input-validation tutorial
Last synced: 14 days ago
JSON representation
Simple C++ console application that prompts the user to enter their age, validates that the input is a number between 1 and 99; otherwise, it displays an error message and re-prompts until a valid age is entered.
- Host: GitHub
- URL: https://github.com/mpluxery/basic-cpp-age-checker
- Owner: mpluxery
- Created: 2025-05-01T14:02:40.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2025-06-02T02:23:18.000Z (27 days ago)
- Last Synced: 2025-06-02T12:08:24.162Z (27 days ago)
- Topics: beginner-code, beginner-friendly, c-plus-plus, cpp, else-if, input-validation, tutorial
- Language: C++
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Valid Age Checker in C++
## Description
This is a simple C++ program that prompts the user to enter their age and validates that the input is a numeric value between 1 and 99, with no extra characters. If the input does not meet these criteria, the program displays an error message and asks again until a valid age is entered. Once a valid number is provided, it outputs a confirmation message including the entered age.## Installation and Build
1. Make sure you have a C++ compiler installed (for example, g++).
2. Save the source code below into a file named `io.cpp`.
3. Open a terminal in the same directory as `io.cpp` and run:g++ io.cpp -o valid_age
Usage
After a successful build, execute the program by typing:./valid_age
When prompted:Enter Age:
type a number between 1 and 99 and press Enter.If the input is invalid (non-numeric characters, numbers outside 1–99, or extra characters), the program will print:
be for real!
and prompt again.Once a valid age is entered, you will see:
You are years old, seems legit!
For example:You are 25 years old, seems legit!
Source Code (io.cpp)#include
#include
#includeint main()
{
int age = 0;
bool valid = false;
char null = '\0';while (!valid)
{
std::cout << "Enter Age: ";std::string line;
std::getline(std::cin, line);std::stringstream is(line);
// Validate:
// 1. Attempt to extract an integer into `age`.
// 2. Check for any extra characters after the number.
// 3. Ensure age is between 1 and 99 (inclusive).
if (!(is >> age) || (is >> std::ws && is.get(null)) || age >= 100 || age <= 0)
std::cout << "be for real!" << std::endl;
else
valid = true;
}std::cout << "You are " << age << " years old, seems legit!" << std::endl;
return 0;
}
How It Works
Variablesage (int): Stores the user’s age.
valid (bool): Becomes true once a valid age is entered.
null (char): Used to detect any extra character after the numeric input.
Input Loop
The while (!valid) loop repeatedly prompts the user to enter their age.
std::getline(std::cin, line) reads the entire line of input as a string.
String Parsing and Validation
A std::stringstream named is is initialized with the input line.
is >> age attempts to parse an integer; if this fails, the input is not a valid number.
(is >> std::ws && is.get(null)) checks for any non-whitespace characters remaining in the stream after parsing the integer (for example, letters or extra digits). If it finds anything, the input is considered invalid.
The conditions age >= 100 || age <= 0 ensure the age is within the range [1, 99].
If any validation step fails, the program prints “be for real!” and repeats the prompt.
Successful Input
When a valid age is provided, the loop exits and the program prints:
You are years old, seems legit!
ExitThe program returns 0 to indicate successful execution.