Help with Whale Talk

#include <iostream>
#include <vector>
#include <string>

int main() {
  // Whale stuff
  std::string normal = "turpentine and turtles";
  
  std::vector<char> whale = {'a', 'e', 'i', 'o', 'u'};
  

  std::vector<std::string> result = {};
  
  for (int i = 0; i < normal.size(); i++)
  {
  	for (int j = 0; j < whale.size(); j++)
    {
      if (normal[i] == whale[j])
      {
        result.push_back(normal[i]);
      }
    }
  }
  
  for (int i = 0; i < result.size(); i++)
  {
    std::cout << result[i];
  }
}

Errors

whale.cpp: In function 'int main()':
whale.cpp:20:35: error: no matching function for call to 'std::vector<
std::__cxx11::basic_string<char> >::push_back(__gnu_cxx::__alloc_trait
s<std::allocator<char> >::value_type&)'
         result.push_back(normal[i]);
                                   ^
In file included from /usr/include/c++/7/vector:64:0,
                 from whale.cpp:2:
/usr/include/c++/7/bits/stl_vector.h:939:7: note: candidate: void std:
:vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::__
cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic
_string<char> >; std::vector<_Tp, _Alloc>::value_type = std::__cxx11::
basic_string<char>]
       push_back(const value_type& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:939:7: note:   no known conversio
n for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<char>
>::value_type {aka char}' to 'const value_type& {aka const std::__cxx1
1::basic_string<char>&}'
/usr/include/c++/7/bits/stl_vector.h:953:7: note: candidate: void std:
:vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&
) [with _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocato
r<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::value_
type = std::__cxx11::basic_string<char>]
       push_back(value_type&& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:953:7: note:   no known conversio
n for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<char>

Or something to that effect (can’t highlight the whole thing)

Anyway, it seems like there’s a problem with push_back, but that’s how it taught me to write it.

2 Likes

When you select the index in a string, like you are doing with normal[i], the result type is a char.

The reason for the error is that you are trying to push a char value into an array that is expecting a value with the type string.

2 Likes

So how do I convert it to string type?

I still get error when I try to .push_back whale[j] (which is not a string, but a char vector)

hale.cpp: In function 'int main()':
whale.cpp:19:34: error: no matching function for call to 'std::vector<
std::__cxx11::basic_string<char> >::push_back(__gnu_cxx::__alloc_trait
s<std::allocator<char> >::value_type&)'
         result.push_back(whale[j]);
                                  ^
In file included from /usr/include/c++/7/vector:64:0,
                 from whale.cpp:2:
/usr/include/c++/7/bits/stl_vector.h:939:7: note: candidate: void std:
:vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::__
cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic
_string<char> >; std::vector<_Tp, _Alloc>::value_type = std::__cxx11::
basic_string<char>]
       push_back(const value_type& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:939:7: note:   no known conversio
n for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<char>
>::value_type {aka char}' to 'const value_type& {aka const std::__cxx1
1::basic_string<char>&}'
/usr/include/c++/7/bits/stl_vector.h:953:7: note: candidate: void std:
:vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&
) [with _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocato
r<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::value_
type = std::__cxx11::basic_string<char>]
       push_back(value_type&& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:953:7: note:   no known conversio
n for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<char>
>::value_type {aka char}' to 'std::vector<std::__cxx11::basic_string<c

1 Like

You will still get the error because the value is still a char.

You have to either convert the char value to a string or change the result vector type to char.

2 Likes

Alright, thanks, that helped

1 Like

let input = “'hi, human”;

const vowels = [“a”, “e”, “i”, “o”, “u”];

let resultArray = ;

for (let i=0; i <input.length; i++){
for (let j=0; j<vowels.length; j++){
if ( input[i] === vowels[j] && input[i] === “e” ) {
resultArray.push(input[i] + input[i] );
}
else if ( input[i] === vowels[j] && input[i] === “u” ){
resultArray.push(input[i] + input[i] );
}
else if( input[i] === vowels[j] ){
resultArray.push(input[i]);
}
}
}

Can somebody please tell me how the 2 for loop works? For example first letter of input is “h” and vowel’s first letter is “a”. when it checks for the second letter in input the letter “i” the 2nd letter in the array is “e” so they aren’t equal. Therefore somehow it pushes that letter into the empty array after its been evaluated that i exists in vowels array?