Whale Talk - Question about the output

Hi! It might be a very simple question but I am still stuck on it:

Why in the Whale Talk program we need a for-loop for the output?

for (int k = 0; k < whale_talk.size(); k++) {
    
    std::cout << whale_talk[k]; 
 }
  
  std::cout << "\n";
}

Why this doesn’t work:

std::cout << whale_talk << " \n";

Here the link to the full program code:

https://github.com/Codecademy/learn-cpp/blob/master/5-vectors/whale.cpp

Thanks!

std::cout stands for character out, as it maps the output to character representations. This puts certain limitations on what can be in the output of this command. As whale_talk is a vector, and a vector is a class, you cannot simply output a whole class as a character representation as that’s not how it’s stored in the computers memory. As such, you have to iterate through and get the contents of the vector, and these can be in the output as they have character representations (be it ints, strings, characters etc).

Once you start learning about classes it should become clearer why you cannot simply ask the program to print out a class, and instead need to print out the contents of a class.

1 Like

pls help i can’t get this to work giving me more chars than in original input.
#include

#include

#include

int main() {

std::string input = “the Dean and dolphins”;

std::vector vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’};

std::vector result;

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]);

  }

}

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

  std::cout << result[k];

}

}

}
gives me a million "aeiou"s

Careful of your use of the assignment operator =, vs the equality operator ==, especially in logic statements. Currently your if is checking the boolean value of input[i] after it is assigned the value of vowels[i].

Also check were you put your for loops. Currently your loop responsible for printing is nested under the loop responsible for looping through input. Which mean it loops once for every time input loops.

1 Like

Thanks. couldn’t figure out what it was doing. :grin:

1 Like