"Whale Talk" project for C++ || Incomplete answer

If I use my code and the provided example (“turpentine and turtles”), it ends up giving me “uueeiee” instead of “uueeieeauuee”. I quickly realized that was because it stops reading the input when it finds a space after “turpentine”. How can get it to pick up the rest of the input too?

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

int main() {

  std::string human_lang = "turpentine and turtle";
  std::vector<char> vowels = {'a', 'e', 'i', 'o', 'u'};
  std::vector<char> results = {};
  
  std::cout << "Enter your human language: ";
  std::cin >> human_lang;
  
  for (int i = 0; i < human_lang.size(); i++) {
    for (int j = 0; j < vowels.size(); j++) {
      if (human_lang[i] == vowels[j]) {
        results.push_back(human_lang[i]);
        if (human_lang[i] == 'e' || human_lang[i] == 'u') {
          results.push_back(human_lang[i]);
        };
      };
    };
  };
  
  for (int k = 0; k < results.size(); k++) {
    std::cout << results[k];
  };
  std::cout << "\n";
  
}

Thanks for any help!

Hello, @rage6.

Well, you’ve gone above and beyond the project instructions. The instructions only ask us to hard code the human_lang value. You included code to get user input, and re-assign the input to your human_lang variable. The issue you’re having is that using std::cin stops reading the input when it reaches the space between turpentine and turtle. If you comment out these two lines:

you’ll see your expected results. Alternatively, you can use the following to assign the entire string typed by the user to human_lang:

std::getline(std::cin, human_lang); //the second argument is the variable to assign the value to

See here for more information on std::getline().

Happy coding!

Edited to use the OP’s variable human_lang in the std::getline() example.

It never crossed my mind that it’s expecting a value built into its own code. Now it makes sense why they have you both declare AND initialize it right out of the gate, though. Thanks!

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.