Thread: Having a problem with a Switch Statement

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    17

    Having a problem with a Switch Statement

    1. Ask for a single character to be typed from the keyboard. Analyze the
    character and print out a message that it is:
    - an alphabetic character (a–z or A–Z)
    - a digit (0–9) or
    - a special character (anything else)

    * Tried a couple of ways of using a switch statement to code this problem just havent been able to figure out an efficient way, the exercise wants me to use an "if" or a "switch statement". I would appreciate your perspective and insight on how to takle this problem...

    thank you

    Arod

    More details...

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[])
    {
        char let;
        
        printf("Please Insert a single character here: \n");
        scanf("%c",&let);
        
        
        switch(let)
        {
            case '0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':
                printf("You Entered a Digit from 0-9\n");
                break;
            case 'a':case'b':case'c':case'd':case'e':case'f':case'g':case'h':case'i':case'j':
            case 'A':case'B':case'C':case'D':case'E':case'F':case'G':case'H':case'I':case'J':
            case 'k':case'l':case'm':case'n':case'o':case'p':case'q':case'r':case's':case't':
            case 'K':case'L':case'M':case'O':case'P':case'Q':case'R':case'S':case'T':case'U':    
                printf("You Entered a Character from A-Z\n");
                break;    
            default:
                printf("Special Character");
        }
        return 0;
    }
    I am going try and do it as an if statement, any suggestions?
    Last edited by ARod609; 07-13-2011 at 04:27 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What did you try?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ARod609 View Post
    I was trying to learn how to use a switch statement, not quite sure if thats the most efficient way though...
    I believe Lase was asking you to post your code...
    (She's not a mind reader... and neither am I)

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Probably not most efficient as switch statement does not allow checking a range of values. Only exact values. You'd need 26 cases for the letters and 10 cases for the numbers.

    You can use 'if' statements to check range of letters: if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'). Similar for digits.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you shouldn't try to read twice. Just read once -- you know the user will type a character; it's not possible for them to type anything else; so read in a character. Then check what kind of thing that character is.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Probably not most efficient as switch statement does not allow checking a range of values. Only exact values. You'd need 26 cases for the letters and 10 cases for the numbers.

    You can use 'if' statements to check range of letters: if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'). Similar for digits.
    Or the C standards isalpha(), isdigit(), ispunct() and isspace()...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    Quote Originally Posted by tabstop View Post
    So you shouldn't try to read twice. Just read once -- you know the user will type a character; it's not possible for them to type anything else; so read in a character. Then check what kind of thing that character is.
    So i Should just use "char" and have one "scanf function"

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ARod609 View Post
    So i Should just use "char" and have one "scanf function"
    Yes, that's certainly what the assignment seems to want. Also, you should read up on the scanf docs (Google "man scanf"). %d is for integers, %f for floats. I'll let you find the right conversion specifier for individual characters .

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    Quote Originally Posted by anduril462 View Post
    Yes, that's certainly what the assignment seems to want. Also, you should read up on the scanf docs (Google "man scanf"). %d is for integers, %f for floats. I'll let you find the right conversion specifier for individual characters .
    Wow! its so much simpler than I thought thank you also the way I set up the Characters A-Z-

    Code:
    case 'a':case'b':case'c':case'd':case'e':case'f':case'h':case'i':case'j':case'k':
                printf("You Entered a Character A-Z\n");
                break;
    Do they have to be individually set up?
    Last edited by ARod609; 07-13-2011 at 02:32 PM.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There is no way to do something like "case 'a'..'z':" in C, you have to list the cases individually like you have been doing. As mentioned by nonoob, you can use if statements:
    Code:
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
        print "You entered A-Z"
    ...
    is certainly more succinct, but relies on the fact that ASCII encoding keeps uppercase, lowercase and digits in sequential blocks This is not the case for encodings like EBCDIC, but those are highly unlikely in your scenario. The way I would prefer is using the standard functions Tater mentioned, but I'm guessing that's not quite what your assignment wanted you to do:
    Code:
    if (islower(c))  // isalpha covers lower and uppercase
        print "You entered A-Z"
    ...

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[])
    {
      
        char let;
        
        
        printf("Please Insert a single character here: \n");
        scanf("%c",&let);
        
        
        if ((let >= 'a' && let <= 'z') || (let >= 'A' && let <= 'Z')) {
            printf("you entered a character from A-Z");
    } 
        
        if (let >= '0' && let <= '9') {
            printf("you entered a digit 0-9\n");
        } 
    
        else {
        printf("you entered a special character");
    }
    
        return 0;
    }
    I tried the "IF" statement it works good, does it look good?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if ... will always run this if check
    if ... will always run this check
    else
        will only run if that was not true
    Compare that with this:
    Code:
    if ... always runs this if check
    else if ... only after the first test was false
    else ... when neither was true...

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    Quote Originally Posted by quzah View Post
    Code:
    if ... will always run this if check
    if ... will always run this check
    else
        will only run if that was not true
    Compare that with this:
    Code:
    if ... always runs this if check
    else if ... only after the first test was false
    else ... when neither was true...
    .
    So that could cause a conflict in more complicated programs?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ARod609 View Post
    So that could cause a conflict in more complicated programs?
    You have to think about what you want your program to do. Watch:
    Code:
    if( x == 1 )
        printf( "x is one\n" );
    else
    if( x == 2 )
        printf( "x is two\n" );
    else
        printf( "x is not one or two\n" );
    Compared to what you have:
    Code:
    if( x == 1 )
        printf( "x is one\n" );
    if( x == 2 )
        printf( "x is two\n" );
    else
        printf( "x is not one or two\n" );
    What is the output of this if x is 1? Is that what you wanted the program to do? You need to think through what you want to do.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    Output
    Code:
    x is one
    x is not one or two
    I see, so the first "if" function and the "else" perform there functions because the code did not clarify what process it wanted to follow?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-30-2011, 01:59 PM
  2. Problem with a switch statement.
    By Merholtz in forum C Programming
    Replies: 3
    Last Post: 10-19-2008, 04:21 PM
  3. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  4. Replies: 1
    Last Post: 08-31-2004, 04:07 AM

Tags for this Thread