Problems with whale.cpp

Hello everyone,
Apologies if this has already cropped up and been answered (I did look, but didn’t see it).
I am working on the Whale Talk section. Hopefully the link will work:
https://www.codecademy.com/courses/learn-c-plus-plus/projects/cpp-whale-talk

Before I put my code below, my problem seems to be that when the for loop looks through the test phrase for vowels, it isn’t identifying the letter “i”. The test phrase was “turpentine and turtles”. I’m also ending up with way too many vowels, even allowing for doubling certain ones, but I will tackle that myself, once I know what I am doing wrong with checking against the vowel vector. Code starts below:

#include
#include
#include

int main() {
// This does not work as I intended. Why doesn’t it pull the letter i?

// Whale, whale, whale.
// What have we got here?
std::string input = “turpentine and turtles”;
std::vector vowels = {‘a’,‘e’,‘i’,‘o’,‘u’};
std::vector result;
std::vector whale_talk;

// loop that iterates through the input string.
for (int i = 0; i < input.size(); i++) {

for (int j = 0; j < vowels.size(); j++) {

if (input[i] == vowels[j]) {
result.push_back(input[i]);
}

}
if (input[i] == ‘u’ || input[i] == ‘e’) {

whale_talk.push_back(input[i]);

}
for (int k = 0; k < whale_talk.size(); k++){

std::cout << whale_talk[k];

}
}
}

Thanks in advance,

David

Hello, @drobbo.

Looks like part way through the project you decided to use whale_talk instead of result. Walk through the steps in your head, and you’ll see why it appears the i's are being skipped. They aren’t, they just aren’t being push_back-ed where you are expecting. Your other issue is with regards to where you located your last for loop. Good luck, and feel free to post again if you need more help! Please review this for future posts: How do I format code in my posts?

1 Like

Thanks @midlindner, much appreciated.
Yes, I will try to get code in the appropriate format in future :slight_smile:
Thanks again,

Dave

1 Like