I have a menu creation system with a loop to check input. When the input is something like "2.4" or "2 4" it accepts "2" as the first input, then I guess "4" gets stored in the buffer and automatically passes as the next input without waiting.
These two solutions are the closest I could find, but I can't figure out how to implement the first (don't even understand most of what they're using) and the second still accepts non-int numbers.
How to make cin take only numbers
relevant code (sorry, still trying to figure out formatting):
int menu(std::vector <std::string> options) //menu creation
{
int input; //user selection
bool check = false; //check for valid input
.
.
.
while(!check)
{
std::cin >> input;
if(std::cin.fail() || input <= 0 || input > static_cast<int>(options.size()))
{
std::cin.clear();
std::cin.ignore();
std::cout << wrongInput << std::endl;
}
else
{
check = true; //change flag for valid input
}
}
return input;
}
I'd like the loop to outright reject anything non-int. I tried having a loop that would check each character sequentially so it would accept multi-digit ints, then fail on a space, "." or character, but it very was clumsy and still didn't work properly.