-
April 9th, 2009, 01:16 AM
#1
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++.
-
April 9th, 2009, 01:29 AM
#2
Re: Cleaner if statement
Originally Posted by zachb
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.
-
April 9th, 2009, 01:48 AM
#3
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.
-
April 9th, 2009, 06:01 AM
#4
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.
-
April 9th, 2009, 06:09 AM
#5
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.
-
April 9th, 2009, 06:14 AM
#6
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.
-
April 9th, 2009, 06:15 AM
#7
Re: Cleaner if statement
Originally Posted by Speedo
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.
-
April 9th, 2009, 07:02 AM
#8
Re: Cleaner if statement
Originally Posted by treuss
Code:
if (strchr("aeiuo", word[START])) // word starts with a lowercase vowel
This seems to me to be by far the best solution.
-
April 9th, 2009, 07:11 AM
#9
Re: Cleaner if statement
Originally Posted by treuss
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.
-
April 9th, 2009, 07:21 AM
#10
Re: Cleaner if statement
Originally Posted by zachb
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.
-
April 9th, 2009, 07:24 AM
#11
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
-
April 9th, 2009, 07:25 AM
#12
Re: Cleaner if statement
Originally Posted by zachb
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.
-
April 9th, 2009, 09:23 AM
#13
Re: Cleaner if statement
Originally Posted by treuss
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|