Draw an outline of an upward-pointing triangle
First of all, please avoid using namespace std;.  It is considered bad practice and will cause many problems.  See Why is using namespace std; considered bad practice? for more information.

Now let's try to simplify the logic.  The following loop:

for (int j = 1; j &lt;= spaces_before; j++) {
  cout &lt;&lt; ' ';
}


is a very verbose way to write

std::cout &lt;&lt; std::string(spaces_before, ' ');


(this happened more than one time.)

And you don't need to keep the variables spaces_before, spaces_after, and star.  Just calculate them on demand.  Also, don't include the special cases in the loop:

#include &lt;iostream&gt;
#include &lt;string&gt;

int main()
{
    std::cout &lt;&lt; "      *\n";
    for (int i = 0; i &lt; 5; ++i)
        std::cout &lt;&lt; std::string(5 - i, ' ') &lt;&lt; '*'
                  &lt;&lt; std::string(2 * i + 1, ' ') &lt;&lt; "*\n";
    std::cout &lt;&lt; std::string(13, '*') &lt;&lt; '\n';
}


This should be enough since you are a beginner.  After this is done, it would be very nice if you extract the magic numbers into named constants.  And the whole process can be wrapped in a function for reuse:

#include &lt;iostream&gt;
#include &lt;string&gt;

void print(std::ostream&amp; os, int rows)
{
    os &lt;&lt; std::string(rows - 1, ' ') &lt;&lt; "*\n";
    for (int i = 1; i &lt; rows - 1; ++i)
        os &lt;&lt; std::string(rows - i - 1, ' ') &lt;&lt; '*'
                  &lt;&lt; std::string(2 * i - 1, ' ') &lt;&lt; "*\n";
    os &lt;&lt; std::string(2 * rows - 1, '*') &lt;&lt; '\n';
}

int main()
{
    print(std::cout, 7);
}