0

Like the function below. can we make the same input validation into a function which takes argument of the datatype we want to take input.

#include<iostream>
#include<string>
#include<limits>
#include <windows.h>
using namespace std;

int main()
{
int testing;
cout<<"Enter a number ";
while (!(cin>>testing))
{
    //Err explanation
    cout<<"PLease enter a numeric type data!"<<endl;
    //clear previous input
    cin.clear();
    //discard previous input
    cin.ignore(numeric_limits<int>::max(),'\n');
    Sleep(1000);
    cout<<"Enter a number ";
}

cout<<"Your number is "<<testing<<endl<<endl;

return 0;
}
  • Does this answer your question? [C++ input validation](https://stackoverflow.com/questions/25666047/c-input-validation) – Zig Razor Oct 17 '20 at 12:06
  • You can just put the code that's described in the duplicates answers into a function, which takes the `std::cin` and the variable you want to extract as reference parameters. – πάντα ῥεῖ Oct 17 '20 at 12:11
  • Please can you show this in code. – Aman Kumar Oct 17 '20 at 12:42
  • Have you found the answer in the link given by @Zig or do you still need the code. – Anon Oct 17 '20 at 13:03
  • Sorry, I'm at very beginner level and the code described there was a bit complex. I know how to do input validation but I want to make it into a function – Aman Kumar Oct 17 '20 at 17:58

0 Answers0