Mastery 27

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:

 

 
 

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:

https://www.youtube.com/watch?v=YIX7UhIKEIk

1017 27

CC BY 4.0 Mastery 27 by Mauricio Cooper is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.