Displaying "C++" in ASCII art with user inputting an integer I have a school assignment and would love it if you can criticize or give a couple of pointers/advice about my piece of code (don't give me the answer, breaks the point of the exercise).



Here's my work:

#include &lt;iostream&gt;
#include &lt;iomanip&gt; 

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int main()
{
//Declaring our variables
int number; //Integer input from the user

cout &lt;&lt; " Please enter an integer :\n"; //Will display text
cin &gt;&gt; number; //The user's input will now be stored in the variable

//These following lines will compose the ascii art which spells out "c++"


cout &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number 
&lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; endl; //line 1

cout &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; setw(17) &lt;&lt; number &lt;&lt; number &lt;&lt; 
setw(17) &lt;&lt; number &lt;&lt; number &lt;&lt; endl; // line 2

cout &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; setw(17) &lt;&lt; number &lt;&lt; number &lt;&lt; 
setw(17) &lt;&lt; number &lt;&lt; number &lt;&lt; endl; // line 3

cout &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; setw(12) &lt;&lt; number &lt;&lt; number &lt;&lt; number 
&lt;&lt; number &lt;&lt; number &lt;&lt; number 
&lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; setw(9) &lt;&lt; number &lt;&lt; 
number &lt;&lt; number &lt;&lt; number
&lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; 
endl; //line4

cout &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; setw(17) &lt;&lt; number &lt;&lt; number &lt;&lt; 
setw(17) &lt;&lt; number &lt;&lt; number &lt;&lt; endl; // line 5

cout &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; setw(17) &lt;&lt; number &lt;&lt; number &lt;&lt; 
setw(17) &lt;&lt; number &lt;&lt; number &lt;&lt; endl; // line 6

cout &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number 
&lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; number &lt;&lt; endl; //line 7

return 0;
}
Graphical representation

From your 'number &lt;&lt; number &lt;&lt; number' thing it is not immediately obvious what is being drawn, and how to fix it in a specific way when needed.

By using the template where you just replace placeholder characters with what you need, you get more WYSIWYG-like result.

Input validation

Your code assumes that user inputs appropriate value. What if user inputs 1234? (You can get more fancy and guard against inputting non-numbers too)

#include &lt;iostream&gt;

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::to_string;

const auto ASCII_template = string("\
SSSSSSSSSSS\n\
SSS               SS           SS\n\
SSS               SS           SS\n\
SSS           SSSSSSSSSS   SSSSSSSSSS\n\
SSS               SS           SS\n\
SSS               SS           SS\n\
SSSSSSSSSSS");


string replace_all(const string&amp; base_string, const string&amp; search, const string&amp; replace) {
  string s = string(base_string);
  size_t pos = s.find(search);
  size_t len = replace.size();
  while ( pos != string::npos ) {
     s.replace( pos, len, replace );
     pos = s.find(search);
  }
  return s;
}

unsigned int get_user_digit() {
  unsigned int result;
  do {
    cout &lt;&lt; "Please enter a single digit:" &lt;&lt; endl;
    cin &gt;&gt; result;
  } while (result &gt;= 10);
  return result;
}

int main() {
  cout &lt;&lt; replace_all(ASCII_template, string("S"), to_string(get_user_digit())) &lt;&lt; endl;
  return 0;
}


You can try it online here.

I don't remember C++ that much, but something along those lines would work. Depending on how much of С++11 you can use, std::regex_replace would simplify replace_all function. If you can not use std::string, it wouldn't change that much either, although you'd have to use some C-like workaround.