#include <iostream>
#include <limits>
void input(unsignedshort& in) {
while(true) {
std::cout << "Enter:\t";
if(std::cin >> in && (in>=0 && in<=9)) {break;}
system("cls");
std::cout << "Input not in range of (0 - 9)" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
int main(int argc, char* argv[]) {
unsignedshort n = 0;
input(n);
std::cout << "n:\t" << n << std::endl;
std::cin.get();
std::cin.sync();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
Note: I'm not advocating the use of system() or std::cin.sync() necessarily. For the sake of simplicity, I've elected to omit a more acceptable "clear screen" function.
If all the values are between 0 - 9 inclusive, an unsigned short should be more than enough to hold those values.
Basically, you pass an unsigned short by reference to the input function, which loops until valid input is provided by the user.
Floating point values will get implicitly cast to an unsigned short.