61 lines
1.4 KiB
C++
61 lines
1.4 KiB
C++
/*
|
|
* File: InputHelper.h
|
|
* Description: Handles input validation and error handling
|
|
* Author: Smitha
|
|
* Created: 08-Apr-2026
|
|
*/
|
|
|
|
#pragma once
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <string>
|
|
#include <stdexcept>
|
|
|
|
namespace util
|
|
{
|
|
/*
|
|
* Function: read
|
|
* Description: Reads input from console into a variable of type T.
|
|
* Parameters:
|
|
* value - reference to a variable of type T where the input will be stored
|
|
* Returns:
|
|
* void - throws runtime_error if input is invalid
|
|
*/
|
|
|
|
template <typename T>
|
|
inline void read(T& value)
|
|
{
|
|
if (!(std::cin >> value))
|
|
{
|
|
std::cin.clear();
|
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
|
throw std::runtime_error("Invalid console input");
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Function: read
|
|
* Description: Reads a line of text input from console into a string.
|
|
* Parameters:
|
|
* value - reference to a string where the input will be stored
|
|
* Returns:
|
|
* void - no return value
|
|
*/
|
|
|
|
inline void read(std::string& value)
|
|
{
|
|
std::getline(std::cin >> std::ws, value);
|
|
}
|
|
|
|
/*
|
|
* Function: pressEnter
|
|
* Description: Pauses execution until the user presses Enter.
|
|
* Parameters: None
|
|
* Returns: void - no return value
|
|
*/
|
|
|
|
inline void pressEnter()
|
|
{
|
|
system("pause");
|
|
}
|
|
} |