There is such a program:

#include <bits/stdc++.h> using std::cout; using std::endl; using std::string; int main() { const int n = 15; for(int i=0;i<n;i++) cout << string(n/2-1-i, ' ') << string(i*2+1, 42) << endl; return 0; } 

But while working, she throws an exception. Question: what are the ways to get rid of it or write a program differently?

  * *** ***** ******* ********* *********** ************* terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_create 

    1 answer 1

    It is clear that n/2-1-i must be non-negative. Just check.

    And what exactly do you want to print? Triangle or rhombus?

    Update

    If the herringbone is what ends in crash, then just stop in time.

      for(int i = 0, d; i < n && (d = n / 2 - 1 - i) >= 0; i++) cout << string(d, ' ') << string(i * 2 + 1, 42) << endl; 

    Or do you need a Christmas tree from n levels? Then this formula is no good.

    • Simple, plain Christmas tree - inceon