I have about 50 different tags, each is at most 2 chars in lengnth. depending on the input of the user a combination of tags will be joined together into one large string. Is there a simpler/more efficient way then having 50 different if-statements? for that matter, how is a switch/case statement different than a series of if statements and, would a hash table be appropriate here?

note -- I have already started creating a bunch of

string tag = "xx";

for each different tag..

Edited by reallyslick: n/a

5 Years
Discussion Span
Last Post by reallyslick

The user will enter something like

>>apples -color -size -weight

but in my case I will have at least 50 different tags. The only thing I can think of is to iterate through a bunch of If's until the [[ users input == "tag" ]] if statement is found. does that help?


hm... The tag 'color' will be sent to the website as 'c'. so currently I have
String Color = "c";
so that when the -Color tag is typed out by the user, the program will iterate through the if's and find the correct one in order to append 'c' to the url.


I would make an array of tags then all the program needs to do is iterate through the array -- a lot easier than creating 50+ if statements. The array will make it a lot easier to add and/or delete tags too.

[edit]arkoenig has a better solution -- use a map.

Edited by Ancient Dragon: n/a


I'm not sure how a "Map works" can u give me a brief? I'll look for some documentation online somewhere.. i trust ur judgement! thank you!


You will find the reference on std::map here.

A simple example that basically fits your problem is this:

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
  map<string,string> tags;
  //create the list of tags:
  tags["-color"] = "c";
  tags["-weight"] = "w";
  tags["-size"] = "s";

  while(true) {
    string input_tag;
    cout << "Please enter a tag (q for quit): ";
    cin >> input_tag;
    if(input_tag == "q")
      return 0;
    cout << "The tag '" << input_tag << "' corresponds to '" << tags[input_tag] << endl;
  return 0;



Thanks folks, You guys have proven to be very helpful -- I extend my gratitude to all of you! -- if i have any more questions i know where to come..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.