CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2009
    Posts
    35

    Cleaner if statement

    Alright here's the deal. I am using a if/else statement to determine whether the letter of the first character in the array is a consonant or a vowel, also if it is the consonant q followed by u, then this must also be known.

    (START = 0)
    Code:
      int i =3;
    if (word[START] == 'a' && word[START] == 'e' && word[START] == 'i' 
    && word[START] == 'o' && word[START] == 'u')
      int i = 1; 
    else if(word [START] =='q' && word[START + 1] == 'u')
      int i = 2;
    It defaults to 3 as everything else will start with just a consonant. Is there some way to write this neater, using simple C++.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Cleaner if statement

    Quote Originally Posted by zachb View Post
    Alright here's the deal. I am using a if/else statement to determine whether the letter of the first character in the array is a consonant or a vowel, also if it is the consonant q followed by u, then this must also be known.

    (START = 0)
    Code:
      int i =3;
    if (word[START] == 'a' && word[START] == 'e' && word[START] == 'i' 
    && word[START] == 'o' && word[START] == 'u')
      int i = 1; 
    else if(word [START] =='q' && word[START + 1] == 'u')
      int i = 2;
    It defaults to 3 as everything else will start with just a consonant. Is there some way to write this neater, using simple C++.
    Hehe.. It actually defaults to 3 because of scope. Take a look at how many times you declare the variable i.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: Cleaner if statement

    Is there some way to write this neater, using simple C++.
    A small inline function is about the best you're going to get

    Code:
    inline bool IsVowel(char c)
    {
      return (c == 'a') || (c == 'e') || (c == 'i') ||
        (c == 'o') || (c == 'u');
    }
    
    if (IsVowel(word[START]))
      ...
    else if (...)
      ...
    But it's probably not even worth doing that unless you are checking if a char is a vowel in multiple places.

  4. #4
    Join Date
    Feb 2009
    Location
    Ukraine
    Posts
    64

    Re: Cleaner if statement

    Code:
    int i =3;
    if (word[START] == 'a' && word[START] == 'e' && word[START] == 'i' 
    && word[START] == 'o' && word[START] == 'u')
      int i = 1; 
    else if(word [START] =='q' && word[START + 1] == 'u')
      int i = 2;
    Those lines are definitions of a new i variable, that overlaps your initial one (the one you are probably want to determine after the if-else checks). You need assignments instead - remove the 'int' keywords as pointed.

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Cleaner if statement

    Code:
    if (strchr("aeiuo", word[START])) // word starts with a lowercase vowel
    Last edited by treuss; April 9th, 2009 at 06:54 AM.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  6. #6
    Join Date
    Feb 2009
    Location
    Ukraine
    Posts
    64

    Re: Cleaner if statement

    And, by the way, your conditions will always be false due to incorrect usage of logical operation 'AND' (&&). You should use 'OR' (||) instead, since word[START] cannot be both, for instance, 'A' and 'E' contemporary.

  7. #7
    Join Date
    Jan 2009
    Posts
    596

    Re: Cleaner if statement

    Quote Originally Posted by Speedo View Post
    A small inline function is about the best you're going to get

    Code:
    inline bool IsVowel(char c)
    {
      return (c == 'a') || (c == 'e') || (c == 'i') ||
        (c == 'o') || (c == 'u');
    }
    
    if (IsVowel(word[START]))
      ...
    else if (...)
      ...
    But it's probably not even worth doing that unless you are checking if a char is a vowel in multiple places.
    Even if it is only currently used in once place, it makes the code much more readable so is worth doing for that reason alone.

  8. #8
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Cleaner if statement

    Quote Originally Posted by treuss View Post
    Code:
    if (strchr("aeiuo", word[START])) // word starts with a lowercase vowel
    This seems to me to be by far the best solution.

  9. #9
    Join Date
    Mar 2009
    Posts
    35

    Re: Cleaner if statement

    Quote Originally Posted by treuss View Post
    Code:
    if (strchr("aeiuo", word[START])) // word starts with a lowercase vowel
    I looked up strchr and it talks about pointers and we haven't been taught that. Could some explain how it works exactly? Could i do this for example.
    Code:
    int i =3;
    if (strchr("aeiuo", word[START])
      i = 1;
    else if (strchr("q", word[START] && strchr("u", word[START + 1])
      i = 2;
    That way I get 3 for consonant unless it stats with a vowel or the first two letters are qu?

    Also isnt the array supposed to come first?

    char * strchr ( const char *, int )
    Last edited by zachb; April 9th, 2009 at 07:18 AM.

  10. #10
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Cleaner if statement

    Quote Originally Posted by zachb View Post
    I looked up strchr and it talks about pointers and we havnt been taught that.
    The only part you need to understand is that strchr returns the null pointer, if word[START] is not found in "aeiuo". The null pointer "returns" false, if evaluated in an if-statement, any other pointer returns true.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Cleaner if statement

    Or

    Code:
    string word;
    
    bool starts_with_vowel = (word.find_first_of("aeiou") == word.begin());
    
    bool starts_with_qu = (word.find("qu") == word.begin());
    See std::string
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  12. #12
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Cleaner if statement

    Quote Originally Posted by zachb View Post
    I looked up strchr and it talks about pointers and we haven't been taught that. Could some explain how it works exactly? Could i do this for example.
    Code:
    int i =3;
    if (strchr("aeiuo", word[START])
      i = 1;
    else if (strchr("q", word[START] && strchr("u", word[START + 1])
      i = 2;
    That way I get 3 for consonant unless it stats with a vowel or the first two letters are qu?

    Also isnt the array supposed to come first?
    Using strchr for the second check does not make sense. Just use == here (as in your original code) or alternatively look up strncmp.

    "aeiou" is the array, it is an array of characters. word[START] is the single char which is searched in the array.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  13. #13
    Join Date
    Mar 2009
    Posts
    35

    Re: Cleaner if statement

    Quote Originally Posted by treuss View Post
    Using strchr for the second check does not make sense. Just use == here (as in your original code) or alternatively look up strncmp.

    "aeiou" is the array, it is an array of characters. word[START] is the single char which is searched in the array.
    Not sure I understand. I need it to return 1 if the vowel is the first letter, 2 if qu is the first second and 3 for all other consonants including a q that isnt followed by u(I dont think this case exists in reality though).
    Last edited by zachb; April 9th, 2009 at 09:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured