Thread: Need help with Changing Switch Function to if/else statements

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    11

    Need help with Changing Switch Function to if/else statements

    I have a switch function but i need to change it to if/else statements that have the same result, please help! I have tried several times but am not getting the result. This is the switch function I need to change:
    Code:
    int check_vowel(char c){   
         switch(c) {
                case 'a':
                case 'A':
                case 'e':
                case 'E':
                case 'i':
                case 'I':
                case 'o':
                case 'O':
                case 'u':
                case 'U':
                  return 1;
                default:
                  return 0;
            }
        }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    if/else statements are generally taught and understood before switch statements due to their simplicity. What have you tried so far? An if statement is as simple as

    Code:
    if (condition)
        /* do things */
    Whereas an else statement would print the falsehood (or truth) of the previous statement. One golden rule to remember is, an else statement will only relate to the last if statement within that scope.
    Double Helix STL

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Possibly hard to explain to your tutor....
    Code:
    int check_vowel(char c) {
      return strchr("AEIOUaeiou",c) != NULL;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by TehQii View Post
    I have tried several times but am not getting the result.
    In the future, you should post what you've tried so we can help you fix the problems.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    11
    Sorry, I've tried
    Code:
    int check_vowel(char c){   
     if(c == 'A'||'a'||'E'||'e'||'I'||'i'||'O'||'o'||'U'||'u'){
            return 1;
        }else{
            return 0;
        }
    }
    but this doesn't seem to work to return the same as the the switch statement when I call the function check_vowel

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can't combine an if() statement like that, you need both sides of the comparison for each comparison.

    Code:
    if(c == 'a' || c == 'b' || c == 'c' || etc...

    Jim

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Also be sure you're supposed to be using the logical or, and not using chained if-else:

    Code:
    if(c == 'a')
        // ...
    else if ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with my switch statements
    By friedsandwich in forum C Programming
    Replies: 18
    Last Post: 12-05-2012, 05:07 PM
  2. switch statements
    By shawnk01 in forum C Programming
    Replies: 3
    Last Post: 07-12-2011, 10:42 PM
  3. Using switch statements in function-like macros
    By robot_chicken in forum C Programming
    Replies: 3
    Last Post: 07-25-2009, 03:09 PM
  4. switch statements.
    By elixon in forum C Programming
    Replies: 2
    Last Post: 11-30-2006, 05:57 PM
  5. Switch Statements
    By guitarman32 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 07:02 AM

Tags for this Thread