Validated user input in C++
For this mastery, I found a super useful video that taught me how to make a validation for a user input
Here is also an example code with a validation user input code:
#include
#include
int main(){
bool valid = false;
int input;
while(!valid){
if(std::cin>>input){//this checks whether an integer was entered
if(input 0) valid = true;//then we have to see if this integer is in range
}else std::cin.clear();//some cleaning up
std::cin.ignore(std::numeric_limits<:streamsize>::max(), 'n');//empty input stream
if(!valid) std::cout "this input is not validn";
}
std::cout " is between 1 and 5n";
std::cin.get();
return 0;
}
Credits: