I'm asking the user to select a choice between items in a backpack by entering an integer corresponding to the item. But despite my current integer input validation code, the whole program terminates instead of redisplaying the choices and asking the user to enter a choice again. Is there anything problematic in the code below that may be causing this?
int num;
do{
std::cout << "Choose item to use." << std::endl;
for(int i = 0; i < backpack->size(); i++){
std::cout << i+1 << ". " << backpack->at(i) << std::endl;
};
std::cin >> num;
if(!std::cin.fail()){
if(num < 0 || num > (backpack->size())){
std::cout << "Plese enter an integer in range." <<std::endl;
}else{
break;
};
}else{
std::cin.clear();
std::cin.ignore(80, '\n');
std::cout << "Invalid input. Please enter an integer." << std::endl;
};
}while(std::cin.fail() || (num<0 || num > (backpack->size())));