Why always begin new lines with std: :cout<<

Hey, so I just completed the hello world Block letters project, and I’m wondering why I would spend all the time it suggests to rewrite std: :cout<< “whatever I want to say here \n”; at the beginning of every line when you can achieve the same results by just not ending the statement with a ; and simply beginning a new line with " more stuff I want to say \n" and then whenever you’re done writing just complete it with “the last thing I want to say”;

I’m brand new to coding so if it’s just to have good form and be more professional I understand I guess I’m just curious.

Hello @magios, welcome to the forums! Could you post the code, formatting it by using the information from this post, and a link to the exercise, please?

Welcome @magios!! :slightly_smiling_face:

As @codeneutrino said, when you ask a question in the forums, posting your code and a link to the lesson will help make your question easier to answer.


The block letters challenge confused me too, and I’m not entirely certain why they built it the way they did. My best guess is that it is simply to give you practice using std::cout << since users are still expected to be rather new to C++ at the point in the course when they give you the challenge.

Another way of looking at it is they simply choice one of several simple ways to display the block letters. One of which is how you proposed:

#include <iostream>

int main() {
  std::cout << " ####\n##  ##\n######\n##  ##\n##  ##\n";
}

The benifit of this method is as that you can condense the whole letter into a single std::cout. However doing this does take away from readability, with only a single letter in my example above, it takes a bit of thought to see what letter is being printed. And If I remember correctly you have to print out multiple letters in the challenge, which could quickly become very difficult to read.
Using the method they use can make it much easier to see what letter you are printing:

#include <iostream>

int main() {
  std::cout << " ####  \n";
  std::cout << "##  ## \n";
  std::cout << "###### \n";
  std::cout << "##  ## \n";
  std::cout << "##  ## \n";
}

// This displays the same as my previous example.

Due to the way it is written, you can easily see what is going to be printed so this may be the reason they chose to write std::cout for every line .


Once you get farther into C++, a bit more advanced way to do this is using a form of multi-line strings by making a variable with they type const char*:

#include <iostream>

int main() {
  const char* block_A = 
  " ####  \n"
  "##  ## \n"
  "###### \n"
  "##  ## \n"
  "##  ## \n";

  std::cout << block_A;
}
3 Likes

Hi so I’m not sure how to edit my original post but here’s a link to the exercise.
Also here’s the exact code I used an why I asked this question.

// This code should spell my Initials in block letters

#include <iostream>

int main() {

std::cout<<

"    o     BBB    \n"

"  o   o   B   B  \n"

" o     o  B B    \n"

"  o   o   B   B  \n"

"    o     BBB    \n";

return 0;

}

I think I understand that at the very least the project was meant to have me practice std::cout<<. WHat you’ve done here with the const character is actually most similar to how I ended up creating my code. which is why I was so confused about the exercise suggesting that I command it to cout so often. sorry for not including this originally, but here’s my code.

// This code should spell my Initials in block letters
#include <iostream>
int main() {
std::cout<<
"    o     BBB    \n"
"  o   o   B   B  \n"
" o     o  B B    \n"
"  o   o   B   B  \n"
"    o     BBB    \n";
return 0;
}

Which looks almost exactly like what you were explaining with const char. I am learning about variables right now so I kind of understand what you’re saying though that seems more advanced than what I’ve learned so far. Thanks for the response though!

1 Like

Excellently done and very creative!
I didn’t learn how to make a multi line string until I was much farther into the course.

Only thing I did differently was create a variable before printing it out to show what type it was.
You can also create the same variable as an array instead of const char*:

#include <iostream>

int main() {
  char block_A[41] = 
  " ####  \n"
  "##  ## \n"
  "###### \n"
  "##  ## \n"
  "##  ## \n";

  std::cout << block_A;
}

If I remember correctly, Codecademy’s lessons only cover char and std::string, so once you get done with the course there is still much to learn about strings. I was actually just talking on the discord server about how complex strings can be in C++.
For now std::string and char should be all you need.

1 Like

Thank you! I’m not sure I’d call it creative so much as lazy… but some dude I can’t remember once said something I probably remember wrong - “I’d give the job to the lazy person cause they’ll do whatever it takes to do as little work as possible but still get the job done.” Lol. I can’t wait to learn more about strings and variables in the future!

1 Like