Like Tree1Likes
  • 1 Post By rags_to_riches

Using switch-case statements!

This is a discussion on Using switch-case statements! within the C Programming forums, part of the General Programming Boards category; So I am trying to create a program that will take an integer from the user if they enter 'E' ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22

    Using switch-case statements!

    So I am trying to create a program that will take an integer from the user if they enter 'E' or 'e' from the menu. So my code looks like this:

    Code:
    // Menu options: C, c, D, d, E, e and saves the option to user1
    
    switch (user1)
        {
               case 'E':
                    printf ("\nPlease enter an integer number: ");
                    scanf ("%d", &user2);
        }
    The question is: Can I do something like...

    Code:
    switch (user1)
        {
               case 'E' || case 'e'
                    printf ("\nPlease enter an integer number: ");
                    scanf ("%d", &user2);
        }
    If not, what might be a way of doing that but without If-Else statements?

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    No, you simply list them on separate lines:

    Code:
    switch (user1)
        {
               case 'E':
               case 'e':
                    printf ("\nPlease enter an integer number: ");
                    scanf ("%d", &user2);
        }
    a better option would be to run the switch variable through toupper or tolower (your choice) before the switch (or as part of the switch) keep the code for the cases from getting cluttered.
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    So that means I would have to put a IF statement to check if the variable is uppercase and if it is then the switch statement should get executed assuming that im using toupper function.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No need for an if statement... Rags example works because case statements will "fall through"... until they hit a break statement...
    Code:
    switch (user1)
        {
               case 'E':     // enter here
               case 'e':     // or here
                    printf ("\nPlease enter an integer number: ");
                    scanf ("%d", &user2);
                    break;  // exit here
    
              //other cases go here
        }
    Experiment with it... you'll see...

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    Yes I experimented and it works perfectly! But I am interested in to see how I can use the tolower function here to make every input to lower case and then proceed as usual.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by SimarGill View Post
    Yes I experimented and it works perfectly! But I am interested in to see how I can use the tolower function here to make every input to lower case and then proceed as usual.
    That's even simpler...
    Code:
    switch (tolower(user1))
        {
               case 'e':      
                    printf ("\nPlease enter an integer number: ");
                    scanf ("%d", &user2);
                    break; 
    
              //other cases go here
        }
    If you don't have your compiler's library documentation, get it... there's usually a ton of really helpful information on all these functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-08-2010, 03:15 PM
  2. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  3. Loop and Case statements
    By Nexcompac in forum C Programming
    Replies: 6
    Last Post: 06-26-2008, 04:11 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. right use of switch(), case and break statements
    By Niara in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2006, 06:29 AM

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