-3

I'm trying to make a simple mad libs program in c++, and i want to check and see if a word that a user entered starts with a vowel, and if it does, change the "a" before the word, to an "an". I've been able to get the first character stored, but it will not compare to the other characters in the If statement. Am i doing this completely wrong?

#include <string>
#include <iostream>
using namespace std;

int main() {
    string adj_3;
    string anN;
        char firstChar;

//      GETTING USER'S WORD 

        cout << "ADJECTIVE: " << endl;
        getline(cin, adj_3);

//      GETTING FIRST CHARACTER

        firstChar = adj_3[0];

//      SEEING IF IT'S A VOWEL (not working)

        if(firstChar == ('a' || 'e' || 'i' || 'o' || 'u' || 'A' || 'E' || 'I' || 'O' || 'U')) {
            anN = "n";
        }
        else {
            cout << "not working" << endl;
        }

    cout << "I am having a" << anN << " " << adj_3 << " time at camp." << endl;

}
  • 2
    You might want to read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Rakete1111 Jul 20 '17 at 15:16
  • 4
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). In it it will show you how to do multiple comparisons. – NathanOliver Jul 20 '17 at 15:16
  • 1
    That's not how operator|| works. – Borgleader Jul 20 '17 at 15:16

4 Answers4

3

The || operator needs to be applied to two arguments, like so:

if (firstChar == 'a' || firstChar == 'e' || firstChar == 'i' || ...)

firstChar == 'a' evaluates to a boolean. firstChar == 'a' || firstChar == 'e' takes the two booleans that results from those two operations, and returns another boolean, which is then fed into the next || operation as the first argument. In this way you can "chain" the || operations until one of them is true, or until they're all false.

See here for examples and explanation.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
1

hnefatl's answer is one way.

You can also use switch case without break statements to check vowel. Something like:

switch(firstChar)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O': 
case 'U': cout<<"Vowel";
}

On top of that switch-case have many advantages over if-else ladder as stated here https://stackoverflow.com/a/1028463/6594779.

svtag
  • 184
  • 4
  • 12
1

Logical operator || combines two boolean expressions, e.g. a==0 || b==1, and returns true if either of the two operands is true. If you pass a single character like 'a' as operand, this will be interpreted as true, since the value of 'a' is 97 and 97 != 0 => true. Hence, your expression ('a' || 'e' || 'i' || 'o' || 'u' || 'A' || 'E' || 'I' || 'O' || 'U') will always be true, and firstchar == (....) is the same as firstchar == true, which will probably give false.

You could write...

if (firstChar == 'a' || firstChar == 'e' || firstChar == 'i' || ...)

or...

if (strchr(firstChar, "aeiouAEIOU") != NULL)) ...
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
1

You can use an array too wherein you store all the vowels and then compare it. Something like shown below:

   char vowels[10]={'a','e','i','o','u','A','E','I','O','U'};
   int flag=0;
   for(int i=0;i<10;i++)
   {
       if(vowels[i]==firstChar)
       {
           flag=1;
           anN="n";
       }
   }
   if(flag==1)
        cout << "I am having a" << anN << " " << adj_3 << " time at camp." << endl;
   else
       cout << "not working" << endl;