Using the Switch statement with Characters?

This is a discussion on Using the Switch statement with Characters? within the C Programming forums, part of the General Programming Boards category; To Whomever Can help Me; Is it possible to use the Switch structure for characters? For example: switch(char) { case ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Unhappy Using the Switch statement with Characters?

    To Whomever Can help Me;

    Is it possible to use the Switch structure for characters? For example:

    switch(char)
    {
    case 1:char = 'b'
    .
    .
    .
    I'm trying to do it, but having trouble. Is this even possible? Any help would be great! Thanks!

    Dave

  2. #2
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    It is possible:
    Code:
    char choice;
    printf("Enter a letter...:");
    scanf("%c", choice);
    switch (choice) {
      case 'a': {
      ...
      }
      case 'b': {
      ...
      }
    }
    try that
    Last edited by Lau; 11-18-2002 at 01:14 PM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yes, chars and ints can be used in switches. But don't name a variable 'char' it's a keyword y'know? Also, the label cannot be a variable name, see below:

    Code:
    char a = 'a';
    char b = 'a';
    
    switch(a){
     case b: a = 'x'; break; // <-- illegal.
     case 't': a = 'z'; break;
     case 12 : a = 'r'; break;
     default: a = 'p';
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 04:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  3. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 07:32 PM
  4. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 08:16 AM
  5. Equivalent of less than in a switch statement?
    By Diamonds in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 08:14 PM

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