Function about Vowel Removal Error

closed account (9hX8C542)
So I was writing a function to remove all the vowels in a string, and I kept getting this error:

1
2
3
4
5
6
7
pa07.cpp:138:39: error: non-const lvalue reference to type 'std::string'
      (aka 'basic_string<char>') cannot bind to a value of unrelated type
      'const char [12]'
    cout << csutilities::removeVowels("Hello wOrld") << endl;
                                      ^~~~~~~~~~~~~
pa07.cpp:110:39: note: passing argument to parameter 'str' here
std::string removeVowels(std::string& str)



I don't really understand it, if anyone could explain it, so I won't do it again, that would be amazing. Thanks for any help!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string removeVowels(std::string& str)
{
    string newWord = "";

    for(unsigned int x = 0; x < str.length(); x++)
    {
        if (str[x] == ('A') || str[x] == ('a') ||
            str[x] == ('E') || str[x] == ('e') ||
            str[x] == ('I') || str[x] == ('i') ||
            str[x] == ('O') || str[x] == ('o') ||
            str[x] == ('U') || str[x] == ('u'))
            newWord = newWord + "";
        else
            newWord = newWord + str[x];
    }//for statement to determine vowel
    return newWord;
}//function to remove vowels 

Last edited on
"Hello wOrld" isn't a string. Looks a bit like a string, sometimes can be used where you really want a string, but it's not a string.

Create an actual std::string object and use that instead.
You are not the first one, see here: http://www.cplusplus.com/forum/beginner/251709/#msg1108161
Topic archived. No new replies allowed.