Switch Statements will not recognize a char comparison

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boltster
    New Member
    • Mar 2010
    • 7

    Switch Statements will not recognize a char comparison

    I wanted to use a switch statement to select from several choices and print the choice.

    The problem I am having is that it does not reconize the case statements the way I would assume they should:

    Intel based, xp pro, borland 4.5

    Code:
    switch (gasType)
    {
         case (('r') || ('R')): printf("Type of gasoline: %s\n",RArray);
         case (('u') || ('U')): printf("Type of gasoline: %s\n",UArray);
         case (('s') || ('S')): printf("Type of gasoline: %s\n",SArray);
    }
    I get an error message saying that the two statements following the first case are duplicates.

    I have also tried to separte them out into individual statements:
    Code:
    switch (gasType)
    {
         case 'r' :  printf("Type of gasoline: %s\n",RArray);
         case 'R' : printf("Type of gasoline: %s\n",RArray);
    }
    This seemed to work... When it encountered the 'R' it work fine skipping 'r' and printing as you would expect. When it encounter the 'r' first it would print and then print a second time because it would see the 'R'. Very strange.

    Any insight would be great...
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The operand for the case statement has to be a single value, not an expression; so you were right to separate them. Your immediate problem is that you need to add break statements to prevent one case from falling through into the next.

    You can get the same effect as the || operator in your first attempt via the following construct:
    Code:
       case 'r':
       case 'R':
          printf("Type of gasoline: %s\n", RArray);
          break;
       case 'u':
       case 'U':
          printf("Type of gasoline: %s\n", UArray);
          break;
       ...
    Another way to get the same effect is:
    Code:
    switch (toupper(gasType)) 
    { 
       case 'R':
          printf("Type of gasoline: %s\n", RArray);
          break;
       case 'U':
          printf("Type of gasoline: %s\n", UArray);
          break;
       ...
    However, in this particular example you're doing almost exactly the same thing in each case. You should be suspicious whenever you find yourself typing the same thing on multiple lines. This code can be simplified to:
    Code:
    const char *gasName;
    ...
    switch (toupper(gasType))
    {
       case 'R':
          gasName = RArray;
          break;
       case 'U':
          gasName = UArray;
          break;
       ...
       default:
          gasName = NULL;
    }
    if (gasName != NULL)
       printf("Type of gasoline: %s\n", gasName);

    Comment

    • boltster
      New Member
      • Mar 2010
      • 7

      #3
      I figured that it had to be something straightforward . After reading your post I went looking further into my text book and 3 sections further it brings up the break; statement use with the switch. I will give this a try and that will help big time.

      Thanks

      Comment

      • boltster
        New Member
        • Mar 2010
        • 7

        #4
        Here is the last bit to get my assignment 100%...

        I am using a separte switch statement to print the day of the week but I keep getting an error stating Undefined symbol 'weekDay' in function printReceipt

        I set it up almost identical to the above examples but can not get it to compile...

        Code:
        char *weekday;
        char *RArray;
        char *UArray;
        char *SArray;
        
        switch ( toupper(gasType))
        {
             case ('R'): printf("Type of gasoline: %s\n",RArray); break;
             case ('U'): printf("Type of gasoline: %s\n",UArray); break;
             case ('S'): printf("Type of gasoline: %s\n",SArray); break;
        }
        
        
        switch (dayNum)
        {
             case 0: weekDay = "Sunday\0"; break;
             case 1: weekDay = "Monday\0"; break;
             case 2: weekDay = "Tuesday\0"; break;
             case 3: weekDay = "Wednesday\0"; break;
             case 4: weekDay = "Thursday\0"; break;
             case 5: weekDay = "Friday\0"; break;
             case 6: weekDay = "Saturday\0"; break;
        }
        printf("Today is : %s\n", weekDay);
        Any ideas?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Defined:
          char *weekday;

          Used
          case 0: weekDay = "Sunday\0"; break;

          C/C++ is case sensitive.

          Comment

          • boltster
            New Member
            • Mar 2010
            • 7

            #6
            Sometimes it's the obvious that gets overlooked. That did the trick (as if you had any doubt...

            Thank you for the quick response to all.

            Very nice site and very nice people. I will be letting my classmates know.

            Comment

            Working...