1D Heat Equation Solver 1.0
Computational Methods Assignment 2025
Loading...
Searching...
No Matches
user_input.h
Go to the documentation of this file.
1#ifndef USER_INPUT_H
2#define USER_INPUT_H
3
4#include <functional>
5#include <iostream>
6#include <limits>
7#include <string>
8
9/**
10 * @file user_input.h
11 * @brief Utility functions for safe user input from the console.
12 *
13 * Provides generic and typed functions to read and validate numeric values
14 * from the standard input stream, with customizable prompts and validation rules.
15 */
16
17/**
18 * @brief Generic template for asking a numeric value from the user.
19 *
20 * This function continuously asks the user for input until a valid number
21 * is provided and passes the validation rule.
22 *
23 * @tparam T Type of number (int, double, etc.)
24 * @param prompt Message shown before user input.
25 * @param isValid Validation function returning true if the input is acceptable.
26 * @param errorMessage Message displayed when validation fails.
27 * @return T The valid numeric value entered by the user.
28 */
29template <typename T>
30T askForNumber(const std::string& prompt,
31 const std::function<bool(T)>& isValid,
32 const std::string& errorMessage)
33{
34 T value;
35 while (true)
36 {
37 std::cout << prompt;
38
39 if (!(std::cin >> value))
40 {
41 if (std::cin.eof())
42 {
43 throw std::runtime_error("End of input stream reached.");
44 }
45 std::cin.clear(); // reset input stream
46 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
47 std::cout << "Invalid input. Please enter a number." << std::endl;
48 continue;
49 }
50
51 if (isValid(value))
52 return value;
53
54 std::cout << errorMessage << std::endl;
55 }
56}
57
58/**
59 * @brief Ask for an integer value from the user.
60 *
61 * @param prompt Message shown before user input.
62 * @param isValid Validation function returning true if the input is acceptable.
63 * @param errorMessage Message displayed when validation fails.
64 * @return int The valid integer entered by the user.
65 */
66int askForInteger(const std::string& prompt,
67 const std::function<bool(int)>& isValid,
68 const std::string& errorMessage);
69
70/**
71 * @brief Ask for a floating-point value from the user.
72 *
73 * @param prompt Message shown before user input.
74 * @param isValid Validation function returning true if the input is acceptable.
75 * @param errorMessage Message displayed when validation fails.
76 * @return double The valid floating-point number entered by the user.
77 */
78double askForDouble(const std::string& prompt,
79 const std::function<bool(double)>& isValid,
80 const std::string& errorMessage);
81
82#endif // USER_INPUT_H
int askForInteger(const std::string &prompt, const std::function< bool(int)> &isValid, const std::string &errorMessage)
Ask for an integer value from the user.
Definition user_input.cpp:6
double askForDouble(const std::string &prompt, const std::function< bool(double)> &isValid, const std::string &errorMessage)
Ask for a floating-point value from the user.
T askForNumber(const std::string &prompt, const std::function< bool(T)> &isValid, const std::string &errorMessage)
Generic template for asking a numeric value from the user.
Definition user_input.h:30