switch with the same char

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey0
    New Member
    • Jan 2008
    • 142

    switch with the same char

    Hello, this code probabily donesn't work. But my aim is find a way to work with character that can be',' or ' ', or '|'.
    s could be: 20 2 3 or 20,2,3 or 20character2cha racter3. The problem is also that it could be 20 2 3 or 20, 2, 3
    If I pass at function myFunc() as character ' ' (blank character) compiler says me that I can't have two ' ' in the switch. How can I outperform this problem? thanks

    Code:
    void myFunc(char* character) {
    int i=0;
     while ( string s = readlineFromAFile()) {[INDENT]switch (s[i]) {
       case character: 
            doSomething();
            i++;
            break;
        case '  ':i++; break;
        default: doOther(); break;
    }[/INDENT]
      }
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I'm not sure I understand your issue - it sounds like you're attempting to create something that will parse a file based on a delimiting character.

    Do you have to use a switch statement? If you are having issues with the case being a space, you could try an if-else chain - especially if you only have three possible characters, or want to pass it a certain character to be a delimiter.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You cna use characters in a switch becuse they are integers. A switch requires an intger. However, you have a case character and that is a char* function argument. You can't use a char* as a case.

      Comment

      Working...