Using a character array in a switch question.

This is a discussion on Using a character array in a switch question. within the C Programming forums, part of the General Programming Boards category; Hi guys, hoping you can help with an assignment I'm working on. I've got most of the program working, but ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Using a character array in a switch question.

    Hi guys, hoping you can help with an assignment I'm working on. I've got most of the program working, but it's just little bits and pieces that need tying up.

    Basically one part of the program requires the user to enter a character for the corresponding room type - so for a rectangular room, the user would enter R or r, and for a circular room the user would enter C or c.

    I've currently got this working in a switch statement rather than an if-then-else. Here in lies the problem. One of the requirements is that when the user is ready to enter the character for the last room, he has to enter either LR/lr for the rectangular room or LC/lc for the circular room.

    I have the room type variable declared as char for character, but obviously only one element would go in there. The problem is, because I'm using the switch statement, the case in the switch will only take one character for example -
    case 'R':
    Which does what it's supposed to. I was thinking it would also work for this -
    case 'LR':

    Can this work? I've researched a little using the switch, but it doesn't really say anything on using a character array in the case section, it just gives a simple example using one character or an integer.

    I was wondering if there was something I was missing or a way to work around this. And also if you can use other controls within a case statement like a loop or selection? I've also attached a sample piece of the code I'm working on.

    Code:
    char rmType;
    
    printf("\nPlease enter the room type: ");  
    scanf("%s", &rmType);
    
    switch (rmType) {
    	case 'R': 
    	case 'r':
    	printf("\nEnter the length & width for the rectangular room: ");
    	scanf("%d%d", &recLength, &recWidth);
    	recArea = recLength * recWidth;
    	recTotalArea = recTotalArea + recArea; //Running total of sq ft area
    	recCost = recTotalArea * costPerSqFt;
    	recCount = recCount + 1; //Keeps a count of rectangular rooms
    	break;
    
    	case 'C':
    	case 'c':
    	printf("\nEnter the radius for the circular room: ");
    	scanf("%d", &radius);
    	circArea = 3.14 * (radius * radius);
    	circTotalArea = circTotalArea + circArea; //Running total of sq ft area
    	circCost = circTotalArea * costPerSqFt;
    	circCount = circCount + 1; //Keeps a count of circular rooms
    	break;
    }
    Thanks for any help.
    Lisa

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't use multi-characters for switches, no.

    You could do case 'L', and inside the case do another read and another switch on 'R' and 'C'.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I believe, in some cases, support for UNICODE chars sets will actually allow you to do this; however as tabstop mentioned, in this case it's more practical to do another read ( getc() )... not to mention your use of scanf is highly prone to buffer-overflow.

    Although I am a bit curious to see if this would work:
    Code:
    int multichar = rmType[0] | (rmType[1] << 8)
    switch( multichar )
    {
      case 'LR':
    ...
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    To tabstop - thanks for answering my question on using multicharacters in switches. I originally had that the user would enter 'L' as an option and then have the user enter the room type again. But as per the specs on the assignment, the user has to enter either 'LR' or 'LC'. It essentially ends a loop and just prints out the information.

    To @nthony - I'm doing a first year course in C programming, and our lecturer hasn't really touched on buffer overflow, although thanks for mentioning it, because I will be researching this for my own better understanding. The code that you posted :
    Code:
    int multichar = rmType[0] | (rmType[1] << 8)
    switch( multichar )
    {
      case 'LR':
    ...
    }
    I'm trying all versions of this, even exactly what you have, but obviously I'm missing something. You've declared the variable multichar as an integer and have the other variable rmType assigned to it. Am I reading that correctly? I get the array of characters from rmType[0] and rmType[1], but the single '|' and the '<<' are a bit confusing. I've never seen that. Could that be the OR sign and <= ?

    Thanks for the help.
    Lisa

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Or, yes, but bitwise or. << is just bit shift. And I suppose what he meant was
    Code:
    int multichar = rmType[1] | (rmType[0] << 8);
    since the first letter has to come first (it gets shifted to the left). I would still recommend my approach, not just because it's my approach, but because it will handle your expected input perfectly (on the assumption you're reading a character at a time).

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Hi guys, just wanted to give you an update. I got the program working correctly. Unfortunately I had to give up the switch and use the if-then-else statements.

    I tried using your way tabstop, but it wouldn't allow me to do a switch within a switch with the same characters. I change the char variable rmType to a character array which would allow me to check the position of 'L' and 'R' and then perform the calculations it needs to do.

    Code:
    if ((rmType[0] == 'L' || rmType[0] == 'l') && (rmType[1] == 'R' || rmType[1] == 'r'))
    @nthony actually gave me that idea with his code, but I changed it up a bit.

    Thanks for the help.

    Lisa

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    No need to ditch the switch...here's something along the lines tabstop suggested.
    Code:
    char rmType;
    
    printf("\nPlease enter the room type: ");  
    scanf("&#37;s", &rmType);
    
    switch (rmType)
    {
        case 'L': case 'l':
             if (*(&rmType+1) == 'R' || *(&rmType+1) == 'r') {
                    printf("\nEnter the length & width for the rectangular room: ");
                    scanf("%d%d", &recLength, &recWidth);
                    recArea = recLength * recWidth;
                    recTotalArea = recTotalArea + recArea; //Running total of sq ft area
                    recCost = recTotalArea * costPerSqFt;
                    recCount = recCount + 1; //Keeps a count of rectangular rooms
             }
             else if (*(&rmType+1) == 'C' || *(&rmType+1) == 'c') {
                    printf("\nEnter the radius for the circular room: ");
                    scanf("%d", &radius);
                    circArea = 3.14 * (radius * radius);
                    circTotalArea = circTotalArea + circArea; //Running total of sq ft area
                    circCost = circTotalArea * costPerSqFt;
                    circCount = circCount + 1; //Keeps a count of circular rooms
             }
             else
                    printf("bad input\n");
             break;
    }
    Last edited by itCbitC; 10-27-2008 at 11:03 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Use array syntax.
    Instead of:
    *(stupid + 1)
    Do:
    good[1]

    And do not use &#37;s to read into a single char. That is what %c is for.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Also you can replace your.......
    Code:
    x = x + 1;
    with....
    Code:
    x++
    And where you have.....
    Code:
    x = x + y;
    You can do...
    Code:
    x += y;

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Thanks for the heads-up folks

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    2
    Thanks itCbitC for the use of the switch. I already handed in my assignment, and waiting for the results. The problem was, I figured there was a way to do it with a swtich, but our lecturer hadn't really gone in depth with that topic.

    I'll keep this close and alter my original code I handed in for the assignment - just to play around with it

    You guys rock!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  2. erase all characters in a character array?
    By diddy02 in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:30 PM
  3. Yet another array question...
    By Raigne in forum C++ Programming
    Replies: 11
    Last Post: 11-13-2008, 01:55 PM
  4. pointer/ array question..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 10-14-2008, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21