0

Trying to check if a user input is equal to either 1, 5, 10, 25, or 100, but I'm not sure how else to do it besides having to type out if(user_input != 1 || user_input != 5 ...etc) , especially since I'm going to be repeating this code many times. I've seen people using

switch(user_input){
    case 1:
    case 5:
    ...etc
    case 100: 
        {do stuff}
        break;
    
    default:
        {etc}
}

but that just seems messy to me. Is there no better way to go about it?

Circlesho
  • 15
  • 4
  • One option is to put the values in a `vector` then use `find` to see if the user input is in the vector. – 001 Sep 03 '21 at 14:53
  • Or a set which has an efficient contains method. – gerum Sep 03 '21 at 14:57
  • @gerum a `set` has significant spin-up costs and often requires hundreds of elements in the `set` to outperform the simple stupidity of a linear search through a `vector`. – user4581301 Sep 03 '21 at 14:59
  • Here is a nice collection of options [C++ Most efficient way to compare a variable to multiple values?](https://stackoverflow.com/questions/15181579/c-most-efficient-way-to-compare-a-variable-to-multiple-values). [This answer](https://stackoverflow.com/a/15181949/4581301) seems most appropriate to your case. – user4581301 Sep 03 '21 at 15:00

0 Answers0