Program crashing at run-time in visual studios

Hello,


This is for an assignment and I am completely lost. The program compiles in visual studio professional 2015 then when I use the the = operator in Matrix.cpp line 78 the program crashes. Visual studios debugging is not helping. Code below.

Thanks in advanced.

Main.cpp
<#include"Matrix.h"


int main()
{
Matrix matrix1;
Matrix matrix2;
int row, col;
cout << "Please enter the size of the first matrix separating the rows and columns by a space (i.e. row col)." << endl;
cin >> row >> col;
matrix1.setRowSize(row);
matrix1.setColSize(col);
matrix1.setName("Matrix 1");
cout << "Please enter the size of the second matrix separating the rows and columns by a space (i.e. row col)." << endl;
cin >> row >> col;
matrix2.setRowSize(row);
matrix2.setColSize(col);
matrix2.setName("Matrix 2");
matrix1.inputValues();
matrix2.inputValues();

matrix1.print();
matrix2.print();
Matrix matrix3;
matrix3=matrix1*matrix2;
matrix3.setName("Matrix 3");
matrix3.print();

return 0;
}>

Matrix.h
<#ifndef MATRIX_H
#define MATRIX_H

#include<iostream>
#include<string>

using namespace std;

const int MAXROW = 100;
const int MAXCOL = 100;

class Matrix
{
private:
string name;
int rowSize;
int colSize;
double matrix[MAXROW][MAXCOL];
public:
Matrix();
Matrix(const Matrix& original);

void setRowSize(const int i);
void setColSize(const int j);
void setName(const string name1);
void inputValues();
void print();

Matrix& operator=(const Matrix& original);
Matrix& operator*(const Matrix& original);

};
#endif
>

Matrix.cpp
<#include"Matrix.h"

Matrix::Matrix()
{
for (int i = 0; i < rowSize; i++)
{
name = "";
rowSize = 0;
colSize = 0;
for (int j = 0; j < colSize; i++)
{
matrix[i][j] = 0;
}
}
}

Matrix::Matrix(const Matrix& original)
{
for (int i = 0; i < rowSize; i++)
{
for (int j = 0; j < colSize; i++)
{
matrix[i][j] = original.matrix[i][j];
}
}
}

void Matrix::setRowSize(const int i)
{
rowSize = i;
}

void Matrix::setColSize(const int j)
{
colSize = j;
}

void Matrix::setName(const string name1)
{
name = name1;
}

void Matrix::inputValues()
{
for (int i = 0; i < rowSize; i++)
{
for (int j = 0; j < colSize; j++)
{
cout << "Please enter the value at possition " << i << ", " << j << "." << endl;
cin >> matrix[i][j];
}
}
}

void Matrix::print()
{
cout << name << ":" << endl;
for (int i = 0; i < rowSize; i++)
{
for (int j = 0; j < rowSize; j++)
{
cout << matrix[i][j];
}
cout << endl;
}

}

Matrix& Matrix::operator=(const Matrix& original)
{
name = original.name;
rowSize = original.rowSize;
colSize = original.colSize;
for (int i = 0; i < original.rowSize; i++)
{
for (int j = 0; j < original.colSize; i++)
{
matrix[i][j] = original.matrix[i][j];
}
}
return *this;
}

Matrix& Matrix::operator*(const Matrix& original)
{
Matrix temp;
if (colSize != original.rowSize)
{
cout << "Cannot multiply these matricies." << endl;
return temp;
}
else if (rowSize == original.colSize)
{
temp.name = (name + "*" + original.name);
temp.rowSize = original.colSize;
temp.colSize = rowSize;
for (int i = 0; i < rowSize; ++i)
{
for (int j = 0; j < original.colSize; ++j)
{
temp.matrix[i][j] = 0;
for (int k = 0; k < colSize; ++k)
{
temp.matrix[i][j] = temp.matrix[i][j] + (matrix[i][k] * original.matrix[k][j]);
}
}
}
}
return temp;
}
>
Last edited on
> for (int j = 0; j < original.colSize; i++)
your index variable is `j', but you are increasing `i'
Oh my gosh. Face palm. Thank you so much!
Particularly while learning C++, if only one compiler is being used, strongly favour the LLVM front-end over the GNU and Microsoft offerings. Ideally compile the code with more than one compiler, with warnings set to a high level, and demanding standard conformance.

With -Wall -Wextra -pedantic-errors, clang++ emits a diagnostic:
warning: variable 'j' used in loop condition not modified in loop body

http://coliru.stacked-crooked.com/a/4e84f062ef57e339
Topic archived. No new replies allowed.