Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ALL-Project-2-Text-Based-Game-Blackmail-/input.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
39 lines (29 sloc)
728 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef INPUT_H | |
#define INPUT_H | |
#include<iostream> | |
#include<string> | |
#include<sstream> | |
#include <limits> | |
/** | |
* This function is adapted from the code provided to us in 4003CEM, as a way to validate the type of input from the player. | |
*/ | |
template<typename T> | |
T input( std::string prompt="" ) | |
{ | |
while(true) | |
{ | |
if( !prompt.empty() ) | |
std::cout << prompt; | |
T value; | |
std::cin >> value; | |
if( std::cin.fail() ) | |
{ | |
std::cin.clear(); | |
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); | |
std::cout<<"The input was incorrect. Try a different one."; | |
continue; | |
} | |
return value; | |
} | |
} | |
#endif |