Thread: Switch..case with a string/char* as expression

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    Switch..case with a string/char* as expression

    I have a piece of code that should open a text file (.ini) with a variable on every line, followed by its value. The code should read a variable name, recognize it and set the appropriate variable in my code to the value stored in the file.

    Here is the code:
    Code:
    void IniParser::Parse(char *fileName)
    {
       ifstream iniFile (fileName);
       if(!iniFile.is_open())
       {
          std::cout << "Could not open ini file: " << fileName << std::endl;
          return;
       }
    
       //char inputStr[128];
       string inputStr;
       
       while(iniFile >> inputStr)
       {
          switch (inputStr) {
             case "blah":
                std::cout << "variable blah was read in the ini file" << std::endl;
                //TODO: Read & set variable
                break;
             default:
                std::cout << "Unknown variable encountered while reading"<< *fileName << std::endl;
                break;
          }
       }
    
    }
    The problem is that it seems that I can't use a stream with my string ("error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)"). I tried to use a char array but that doesn't seem to work either. Is something like this possible, or should I use regular if/else with strcmp?

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Use getline to read in the contents of a text file one line at a time:
    Code:
    std::string inputStr;
    while (std::getline(iniFile ,inputStr))
    {
      //do stuff
    }
    The case statement takes a numeric value - a single char meets this criteria but not a string, whether as a char array, the stl std::string or some self-devised variant.

    Since you're using C++ then I'd suggest you use the std::string == operator rather than fiddling around with strcmp:
    Code:
    if (inputStr == "blah")
    {
        std::cout << "variable blah was read in the ini file" << std::endl;
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Basic Data types continue
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 10:21 AM
  5. enumeration with switch case?
    By Shadow12345 in forum C++ Programming
    Replies: 17
    Last Post: 09-26-2002, 04:57 PM