I have a problem with Switches..

This is a discussion on I have a problem with Switches.. within the C Programming forums, part of the General Programming Boards category; I want to make a switch program where letters are choices. I already know that you must use the newline ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    I have a problem with Switches..

    I want to make a switch program where letters are choices. I already know that you must use the newline technique but I wanted to know how you would use both upper case and lower case letters without writing a completely different case for each letter.

    Example:

    Code:
    switch(choice)
          {
    	 case 'A': 
    	    sum = num1 + num2;
    	    printf("The sum of %d and %d is %d.\n", num1, num2, sum);
    	    break;

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    By default, a case will "fall through" to the next case. That's why you typically end each case with a break statement. So to handle both upper and lowercase letters you can use the fall through behavior to your advantage:
    Code:
    switch(choice)
    {
      case 'a':  // Fall through to 'A' case because of lack of break;
      case 'A':
        // Do stuff for 'a' or 'A'
        break;
    }
    Alternatively, you could force the choice to one case or the other. Something like:
    Code:
    switch(toupper(choice))
    {
      case 'A':
        // Do stuff for 'a' or 'A'
        break;
    }
    If you decide to use toupper() make sure you #include <ctype.h>
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    Quote Originally Posted by itsme86
    By default, a case will "fall through" to the next case. That's why you typically end each case with a break statement. So to handle both upper and lowercase letters you can use the fall through behavior to your advantage:
    Code:
    switch(choice)
    {
      case 'a':  // Fall through to 'A' case because of lack of break;
      case 'A':
        // Do stuff for 'a' or 'A'
        break;
    }
    Alternatively, you could force the choice to one case or the other. Something like:
    Code:
    switch(toupper(choice))
    {
      case 'A':
        // Do stuff for 'a' or 'A'
        break;
    }
    If you decide to use toupper() make sure you #include <ctype.h>
    Thank you very much.. I will try that..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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