Help with Or statements in If loop

Hi, I am doing a pig latin converting program. I'm trying to check for vowels, but for some reason the program ignores my or statements. When I just type

if (wordChar == 'a'),
then type cout << wordChar;

wordChar is 'a' and working fine, but when I leave it as it is below in the code. wordChar is displayed as the first letter of the word. Can someone explain what I did wrong.

1
2
3
4
5
6
7
8
9
10
11
12
	   for (int i = 0; i <= (word.length()-1); i++)
	   {
	      wordChar = word.at(i);
	      if (wordChar == 'a' || 'e' || 'i' || 'o' || 'u')
              {
                 cout << wordChar << endl;
                 suffix.assign(word, 0, wordChar);
                 prefix.assign(word, wordChar, word.length());
                 pigLatinWord = prefix + "-" + suffix + "ay";
                 goto exitLoops;
              }      
           }
if (wordChar == 'a' || 'e' || 'i' || 'o' || 'u')

should be :

if (wordChar == 'a' || wordChar == 'e' || wordChar == 'i' || wordChar == 'o' || wordChar == 'u')

Edit: DO NOT USE GOTO in this case you don't need anything there.
Last edited on
IdeasMan is correct.

In more detail, what you are doing in your original code is testing the value of 5 different expressions:

1. wordChar == 'a'
2. 'e'
3. 'i'
4. 'o'
5. 'u'

Expressions 2 - 5 are always non-zero, and are therefore always true. The expression wordChar == 'a' || 'e' || 'i' || 'o' || 'u' is therefore always true.

And goto is something to be avoided, generally. Only use it if you really, really need to. And if you think you really, really need to, it's likely that you need to reconsider the design and structure of your code. In this case, it's utterly unnecessary - there are much better ways to force an early exit from a loop.
Last edited on
Topic archived. No new replies allowed.