Skip to content

Instantly share code, notes, and snippets.

@jiqiujia
Created June 4, 2023 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiqiujia/3aa68cc581b1dbdb4a0b61d77cb6e473 to your computer and use it in GitHub Desktop.
Save jiqiujia/3aa68cc581b1dbdb4a0b61d77cb6e473 to your computer and use it in GitHub Desktop.
c++ code snippets
/*
* function to validate input to be of type `T`
*/
template<typename T>
T& validateInput(T& val, string prompt)
{
while (true) {
cout << prompt;
if (cin >> val) {
break;
}
else {
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
}
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment