I'm sure this is pretty simple, but I couldn't really devise a search query which helped me resolve the issue.

I'd almost be inclined to think this was a bug in the Windows command prompt, except that I've never seen it before, until I started using exceptions, where it occurs if and only if I use exception::what().

This is for a homework assignment, and the program is supposed to compute a series of given problems and display the answers. All of the problems are in a similar vein (matrix/vector arithmetic), and the only ones which cause problems are the problems which are intentionally designed to generate errors, since that's the only time exception::what() is used. 

Here's one of the offending problems:

(By the way, is it OK to arbitrarily place these
problems into blocks so that the objects go out of scope and the destructors are called before the next problem, as I've done?)

```
{ // Problem #9
    Vector v1(5);
    Matrix m1(3, 3, 1);
    try {
        v1.set(1, -2);
        v1.set(2, -1);
        v1.set(3, 4);
        v1.set(4, 9);
        v1.set(5, 3);
        m1.set(1, 1, 12);
        m1.set(1, 2, 36);
        m1.set(1, 3, -7);
        m1.set(2, 1, 4);
        m1.set(2, 3, 11);
        m1.set(3, 1, 7);
        m1.set(3, 2, -5);
        m1.set(3, 3, -2);
        Vector * ans9 = product(m1, v1);
        cout << "Answer to problem 9:" << endl;
        ans9->print();
        delete ans9;
    }
    catch(exception & ex) {
        cout << "Exception in problem 9: " << ex.what() << endl;
    }
} // End problem 9
cout << endl << endl;

```

The Matrix class and its constructor are nothing special, and the code doesn't throw any exceptions there, so I'll just share the offending product() function:

```
Vector * product(Matrix &m, Vector &v) {
    unsigned int vLength = v.getLength(), mRows = m.getRows(), mCols = m.getCols();
    if ( mCols != vLength ) {
            throw std::logic_error("matrix/vector product impossible (size mismatch)!");
    }
    Vector * vprod = new Vector(mRows);
    for (unsigned int i = 1; i <= mRows; ++i) {
        double value = 0;
        for (unsigned int j = 1; j <= vLength; ++j) {
            value += (m.get(i, j)) * (v.get(j));
        }
        vprod->set(i, value);
    }

    return vprod;
}

```

And here's an example of the kind of output I get:

[![screenshot](https://i.stack.imgur.com/hbZGq.png)](https://i.stack.imgur.com/hbZGq.png)

I left that ! in there so you can see that it is just printing whatever the last character was right on down that column, until some other character is explicitly printed there.

So, what exactly is going on here? I figure it's probably something to do with string termination, but maybe that's just because I've had too much fun with C in the past.

EDIT: Folks asked for a compilable code segment, and the best I could do was 228 lines. Here goes:

```
#include <iostream>
#include <iomanip>
#include <cstdlib>

using std::cout;
using std::endl;
using std::exception;

class Vector {
    private:
    unsigned int length;
    double *elements;

    public:
    Vector(unsigned int desiredLength);

    ~Vector();

    //void dDestroy(Vector &v);

    unsigned int getLength();

    void set(unsigned int position, double value);

    double get(unsigned int position);

    void print();
};

Vector::Vector(unsigned int desiredLength) {
    length = desiredLength;
    elements = new double[length];
    for (unsigned int i = 0; i < length; ++i) {
        elements[i] = 0;
    }
}

Vector::~Vector() {
    delete[] elements;
}

unsigned int Vector::getLength() {
    return length;
}                    

void Vector::set(unsigned int position, double value) {
    if (position > length || position <= 0) {
        throw std::logic_error("vector set failed (out of range)");
    }
    --position;
    elements[position] = value;
}

double Vector::get(unsigned int position) {
    if (position > length || position <= 0) {
        throw std::logic_error("vector get failed (out of range)");
    }
    --position;
    return elements[position];
}

void Vector::print() {
    std::cout << "[  ";
    for (unsigned int i=0; i < length; ++i) {
        std::cout << elements[i] << "  " ;
    }
    std::cout << "]";
}

class Matrix {
    private:
    unsigned int rows, cols;
    double **elements;

    public:

    Matrix(unsigned int desiredRows, unsigned int desiredCols, double defaultValue);

    ~Matrix();

    unsigned int getRows();

    unsigned int getCols();

    void set(unsigned int i, unsigned int j, double value);

    double get(unsigned int i, unsigned int j);

    void print();
};

Matrix::Matrix(unsigned int desiredRows, unsigned int desiredCols, double defaultValue) {
    rows = desiredRows, cols = desiredCols;
    // Create
    elements = new double*[rows];
    for (unsigned int i = 0; i < rows; ++i) {
        elements[i] = new double[cols];
    }
    // Initialize
    for (unsigned int i = 0; i < rows; ++i) {
        for (unsigned int j = 0; j < cols; ++j) {
            elements[i][j] = defaultValue;
        }
    }
}

Matrix::~Matrix() {
    for (unsigned int i = 0; i < rows; ++i) {
        delete[] elements[i];
    }
    delete[] elements;
}

unsigned int Matrix::getRows() {
    return rows;
}

unsigned int Matrix::getCols() {
    return cols;
}

void Matrix::set(unsigned int i, unsigned int j, double value) {
    if (i > rows || j > cols) {
        throw std::logic_error("matrix set failed (out of range).");
    }
    --i, --j;
    elements[i][j] = value;
}

double Matrix::get(unsigned int i, unsigned int j) {
    if (i > rows || j > cols || i <= 0 || j <= 0) {
        throw std::logic_error("matrix get failed (out of range).");
    }
    --i, --j;
    return elements[i][j];
}

void Matrix::print() {
    // TODO it would be nice to format based on maximum digits in any value
    for (unsigned int i = 0; i < rows; ++i) {
        std::cout << "[  ";
        for (unsigned int j = 0; j < cols; ++j) {
            std::cout << std::setprecision(2) << elements[i][j] << "  ";
        }
        std::cout << "]\n";

    }
}

Vector * dot(Vector &v1, Vector &v2) {
    if (v1.getLength() != v2.getLength() ) {
        throw std::logic_error("dot product impossible (length mismatch)");
    }
    double result = 0;
    for (unsigned int i = 1; i <= v1.getLength(); ++i) {
        result += (v1.get(i) * v2.get(i));
    }
    Vector * vdot = new Vector(1);
    vdot->set(1, result);
    return vdot;
}

Vector * product(Matrix &m, Vector &v) {
    unsigned int vLength = v.getLength(), mRows = m.getRows(), mCols = m.getCols();
    if ( mCols != vLength ) {
        throw std::logic_error("matrix/vector product impossible (size mismatch)");
    }
    Vector * vprod = new Vector(mRows);
    for (unsigned int i = 1; i <= mRows; ++i) {
        double value = 0;
        for (unsigned int j = 1; j <= vLength; ++j) {
            value += (m.get(i, j)) * (v.get(j));
        }
        vprod->set(i, value);
    }

    return vprod;
}

Vector * dot(Vector &v1, Vector &v2);
Vector * product(Matrix &m, Vector &v);

int main() {
    cout << endl;

    { // Problem #1
    Vector v1(3), v2(3);
    try {
        v1.set(1, 2);
        v1.set(2, 1);
        v1.set(3, 3);
        v2.set(1, 0);
        v2.set(2, 4);
        v2.set(3, -9);

        Vector * ans1 = dot(v1, v2);
        cout << "Answer to problem 1:" << endl;
        ans1->print();
        delete ans1;
    }

    catch(const exception & ex) {
        cout << "Exception in problem 1: " << ex.what() << endl;
    }
    } // End problem 1
    cout << endl << endl;

    { // Problem #2
    Vector v1(2), v2(3);
    try {
        v1.set(1, 12);
        v1.set(2, 1);
        v2.set(1, 3);
        v2.set(2, -1);
        v2.set(3, 5);
        Vector * ans2 = dot(v1, v2);
        cout << "Answer to problem 2:" << endl;
        ans2->print();
        delete ans2;
    }
    catch(const exception & ex) {
        cout << "Exception in problem 2: " << ex.what() << endl;
    }

    } // End problem 2
    cout << endl << endl;
}

```