0

I am making a simple program where if you type 'north' it will subtract a number from your stamina. I got that too work, but there are a lot of possible input to represent 'north' you can have 'no', 'n','NORTH','NoRtH' as you can see this gets tedious after a while, if I have to create an if statement for each and every single possible variation of the word north, and not to mention the other directions I want to use, it won't look nice

I was wondering is there a way to be able to store all those possible variations of that word into something, and when when I make an if statement, it directs it to that storage unit to compare with all possible variations... because I just know that if i make 40+ if and else if statements to compare the users input with all those variations, the code will get ugly and fast.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Uys of Spades
  • 93
  • 1
  • 7
  • Make a mapping of commands to functionoids, and convert all strings to lowercase before doing the lookup. – Mooing Duck Feb 14 '14 at 21:59
  • Can you please elaborate with simple examples? – Uys of Spades Feb 14 '14 at 22:00
  • 1
    http://coliru.stacked-crooked.com/a/4c79ebe45caf2647 (Warning, this prioritizes commands that were loaded later for abreviations. If `north` is loaded and then `nod`, then `n` will correspond with `nod`, not `north`.) – Mooing Duck Feb 14 '14 at 22:11
  • man that looks like chinese lol.. Thanks for the sample code i will go through it and try to make sense of it :D – Uys of Spades Feb 14 '14 at 22:19
  • You could try to have a `std::map` of the command as key and a function pointer as the value like this question does [http://stackoverflow.com/questions/2136998/using-a-stl-map-of-function-pointers](http://stackoverflow.com/questions/2136998/using-a-stl-map-of-function-pointers). Also here is reference for `std::map` [http://www.cplusplus.com/reference/map/map/?kw=map](http://www.cplusplus.com/reference/map/map/?kw=map) – John Odom Feb 14 '14 at 22:23

2 Answers2

2

It sounds like you want to see if a string is contained within an arbitrary set of strings. The C++ standard libraries have functionality for this.

I would use a std::set to contain a list of valid terms, like so:

std::set<std::string> north_terms_set;
north_terms_set.insert("n");
north_terms_set.insert("north");

and then check to see if a given input belongs to the set as follows:

//Convert input to lowercase
std::string input_str = "NoRtH"; 
std::transform(input_str.begin(), input_str.end(), input_str.begin(), ::tolower);

//compare lowercase version of input to the set of valid terms.
if (north_terms_set.find(input_str) != north_terms_set.end()) {
  //User has typed something like 'north', subtract from stamina, etc.
}

While the method suggested by 111111 is simpler, this methods enables you to limit which variants are considered valid and also lets you allow alternatives such as 'north', 'up', 'top', etc.

Alan
  • 3,307
  • 1
  • 19
  • 22
1

If you want to match the following to north:

n, no, nor, nort, north

including their capitalised counter parts you can do this if you use boost string algorithms, you can obviously write your own to_lower and starts_with if you can't use boost for some reason.

std::cin >> input;
boost::to_lower(input);
if(boost::starts_with("north", input)) {
    //match
}
111111
  • 15,686
  • 6
  • 47
  • 62