How to Create a Type Validation Function?

So I am writing an assignment to Detect prime numbers and it works by Asking for how many values you are going to enter, and then saying "Enter value 1: "
then you would input and it would calculate via for loop, and that part is working. However to make my program more foolproof, I devised a way for the user to be unable to "Break" the program by inputting characters or float values.
he is the code for that:

P.S. i have made the messages in this code more generic for better understanding

1
2
3
4
5
6
7
while(!(cin >> num)){ //num is some type (char, float, int etc.)
             cout << "That is not at valid input, please try again" << endl; //"Error Message"
             cin.clear(); 
             cin.ignore(10000, '\n'); //Clear and reset cin
             cout << "Enter value " << n << ": "; //Re-Prompt User for input
             //n is whatever value the for loop is on
}

and this code works fine, I was just curious about how i would turn it into a function. Preferably wiht the name: ValidateInput(Param1, Param2);


The Parameters of the function preferably would be the variable youre inputting and the message you want to prompt. So somehow i wish to have it so for the above example it would look like:

ValidateInput(num, "Enter Value " << n << ": ");

But i dont know exaclty how to lable either parameter part because I want it to work for chars, ints, floats etc. And I dont know what I want it to return if anything either.

Any help would be appreciated!
Topic archived. No new replies allowed.