0

I am just a beginner in C++ and I am trying to scan the first character of an inputted string for vowels using simple syntaxes

I tried using array of strings for vowels

string vowels[] = {"a","e","i","o","u"};
string word;
string first;

cout<< "enter a word: ";
cin>>word;
cout<<endl;

first = word.substr(0,1);

cout<<first<<endl;

if (first == vowels)
{
    cout << "NO!"<<endl;
}
else
{
    cout<< word <<endl;
}


return 0;

if condition yields an error:

Invalid operands to binary expression ('string' (aka 'basic_string, allocator >') and 'string *' (aka 'basic_string, allocator > *'))

1

2 Answers 2

0

You're trying to too see if a word(first) is identical to an array (vowels).

What you need to be doing is check if each character inside the word is also contained inside the vowel array.

A simple function do this would look something like.

bool insideArray( std::string aWord, std::string aArray[] )
{
    for(unsigned int i = 0; i< aWord.length(); i++ ) 
    {
        //Use the find function.
        bool exists = std::find( std:begin(aArray, std:end(aArray), aWord[i]) != std:end(aWord[i])
        if (exists) {
            return true;
        } 
    }
    // reached the end, and have never returned true, so no character from aWord is inside aArray
    return true
}
Sign up to request clarification or add additional context in comments.

Comments

-1

You need to compare first to each element of vowels[].

for(int i=0; i<vowels.size(); i++) {
    if(first == vowels[i])
        // ...
}

4 Comments

I am getting an error: Member reference base type 'string [5]' is not a structure or union
vowels is an array, so vowels.size() won't work. That said, @SydneyBorn should prefer std::vector<std::string> over a raw array.
array::size() is added in C++11. Perhaps you could use (sizeof(vowels)/sizeof(*vowels)) to determine the array length instead.
@LegendofPedro OP is using a raw array, so std::array::size won't help either. A better way to get the length of a raw array (and any std container) is std::size (since C++17)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.