Thread: learning switch

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    14

    learning switch

    Hello programmers!
    i am learning switch, and i gave myself a problem to solve. I want the user to enter 4, then, the program will ask him to enter a character. Everytime i enter 4 the program stops and don't go to the other switch. i have tried many forms before i come here. i would like you guys to help me explain my problem and a little bit about using two switch and a switch in another switch. here is the codes. thank you
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
     int main()
    {
        int a;
        char b;
        scanf("%d",&a);
        switch (a) {
            case 1: {
                printf("1\n");
                break;
            }
            case 2:{
                printf("2\n");
                break;
            }
            case 3: {
                printf("3\n");
                break;
            }
            case 4: {
                scanf("%c",&b);
                break;
            }
            switch(b){
            case 'a': {
                printf("a\n");
                break;
            }
            case 'b':{
                 printf("b\n");
                 break;
            }
            default: {
                printf("haha");
                break;
            }
        }
    
    
        default: {
            printf(" lol");
            break;
        }
        }
        return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What the manual says about the scanf format, %c:
    Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating null byte is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.
    scanf(3): input format conversion - Linux man page

    You may be wondering how this applies to the above program. When you enter a number because scanf("%d", &a); was called, you also type ENTER to give the final version of the input to the program. This ENTER key becomes \n, which is a whitespace character. As you read above, it is not ignored.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    i have no idea what you are talking about. And i just went to that link and still didn't get a clue. Care to explain a little bit more in an easy understanding way for a newbie to programming? thank you.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, when you entered "4", the "4" was read with the first scanf and hence 4 was stored in a. In the switch, this 4 matched the case 4, so the next scanf was executed. This scanf then read the newline character from entering the "4". What you want rather is for this newline to be ignored such that the scanf would read a new char entered by the user. Hence, the solution mentioned in whiteflags' post #2: "To skip white space first, use an explicit space in the format."
    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

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Also, isn't the second switch, "switch(b)" unreachable?

    -

  6. #6
    spaghetticode
    Guest
    megafiddle - I guess so.

    endrick, you may need to move your second switch out of your first one.

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by spaghetticode View Post
    endrick, you may need to move your second switch out of your first one.
    This is not what endrick wants. If the user type 4, it will scan for a character. The second switch should only be executed, if the user type 4.
    The correct way is:
    Code:
    …
            case 4: {
                scanf(" %c",&b);
                switch(b) {
                    case 'a': {
                        printf("a\n");
                        break;
                    }
                    case 'b': {
                        printf("b\n");
                        break;
                    }
                    default: {
                        printf("haha\n");
                        break;
                    }
                }
                break;
            }
    
    …
    Other have classes, we are class

  8. #8
    spaghetticode
    Guest
    Quote Originally Posted by WoodSTokk View Post
    This is not what endrick wants. If the user type 4, it will scan for a character. The second switch should only be executed, if the user type 4.
    I got that. But I was mistaken about the control flow, I have no clue what I have been thinking. You're of course right, the second switch needs to be nested.

  9. #9
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    Thank you. i saw the problem, but i have to enter 4b or 4a to access the second switch. anyway i found a way to do it. thank you. i will post it here.

  10. #10
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    You guys are awesome. i am learning C language and i'm happy with the help i get from you guys. Anyway, based on your explanations i found another way to do it even though it is not a professional way of thinking but it works. Again you guys can look at this code and prolly suggest other ways to do it my simple easy way is like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
     int main()
    {
        int a;
        char b,c;
        scanf("%d",&a);
        switch (a) {
            case 1: {
                printf("1\n");
                break;
            }
            case 2:{
                printf("2\n");
                break;
            }
            case 3: {
                printf("3\n");
                break;
            }
            case 4: {
                scanf("%c",&c);
                printf("enter a letter");
                scanf("%c",&b);
    
    
    
    
            switch(b){
            case 'a': {
                printf("a\n");
                break;
            }
            case 'b':{
                 printf("b\n");
                 break;
            }
            default: {
                printf("haha");
                break;
            }
            }
            break;
        }
    
    
    
    
        default: {
            printf(" lol");
            break;
        }
        }
        getch();
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Look closer at my post #7. In the format for scanf, i have a space in front of %c.
    This space kill all whitespaces in front of the first character.
    Other have classes, we are class

  12. #12
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    OMG i didn't see that i'm very sorry. You are a genius. I wonder when i will be able to correct others lol

  13. #13
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Also, in your new program, the indentation is a bit messed up. If someone was reading through it quickly, it looks like your switch(b) is by itself, when in reality, it's part of your case 4: statement. Post 7 by WoodSTokk shows how it should properly be indented.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch case learning doubt
    By thannara123 in forum C Programming
    Replies: 5
    Last Post: 08-06-2015, 09:23 AM
  2. Learning
    By nojoegohome in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2006, 04:33 AM
  3. Learning Dos and learning Windows
    By blankstare77 in forum C++ Programming
    Replies: 8
    Last Post: 07-31-2005, 03:48 PM
  4. A switch that doesn't switch
    By Death_Wraith in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2004, 12:18 PM

Tags for this Thread