I am a beginner in C++ and I am trying to create a matrix calculator for addition and subtraction, 2Ddynamic array while avoiding to use #include <vector>
I wrote this so far and every time I run it in Visual Studio 2010 I get the following error:
Debug Error!
Program...this one
path\projectfile\thisprogram.exe
#include <iostream>
#include <cctype>
class mathvar
{ private:
int ROWS_A;
int COLUMNS_A;
int i , j;
int ROWS_B;
int COLUMNS_B;
int h, k;
char choice;
public:
mathvar();
~mathvar();
int array_sum();
int array_rest();
char menu();
};
mathvar::mathvar()
{
int ROWS_A = 0;
int COLUMNS_A = 0;
int ROWS_B = 0;
int COLUMNS_B = 0;
}
usingnamespace std;
int main()
{
mathvar calculator;
calculator.menu();
return 0;
}
int mathvar::array_sum()
{
cout<<"Enter values for Array A"<<endl;
cout<<"Enter a number for ROWS: ";
cin>>ROWS_A;
cout<<"Enter a number for COLUMNS: ";
cin>>COLUMNS_A;
int **dynamicArray_A = newint*[ROWS_A];
int **dynamicArray_B = newint*[ROWS_B];
for (i = 0; i < ROWS_A; i++)
{
dynamicArray_A[i] = newint[COLUMNS_A];
cout<<"Enter elements for row number "<<i+1<<": "<<endl;
for (j = 0; j < COLUMNS_A; j++)
{
cout<<"Number "<<j+1<<": ";
cin>>*(dynamicArray_A[i] + j);
}
}//end for for dynamic memory alloc for array A
cout<<"Enter values for Array B"<<endl;
cout<<"Enter a number for ROWS: ";
cin>>ROWS_B;
cout<<"Enter a number for COLUMNS: ";
cin>>COLUMNS_B;
for (h = 0; h < ROWS_B; h++)
{
dynamicArray_B[h] = newint[COLUMNS_B];
cout<<"Enter elements for row number "<<h+1<<": "<<endl;
for (k = 0; k < COLUMNS_B; k++)
{
cout<<"Number "<<k+1<<": ";
cin>>*(dynamicArray_B[h] + k);
}
}//end for for dynamic memory alloc for array B
cout<<"Matrix A"<<endl;
for (i = 0; i < ROWS_A; i++)//prints Array A
{
for (j = 0; j < COLUMNS_A; j++)
{
cout<<*(dynamicArray_A[i] + j)<<"\t";
}
cout<<endl;//forces to next line
}//end of print array A
cout<<endl;
cout<<" + "<<endl;
cout<<"Matrix B"<<endl;
for (i = 0; i < ROWS_B; i++)//prints Array B
{
for (j = 0; j < COLUMNS_B; j++)
{
cout<<*(dynamicArray_B[i] + j)<<"\t";
}
cout<<endl;//forces to next line
}//end of print array B
cout<<" = "<<endl;
for (int count = 0; count < ROWS_B; count++)
{
for (int count2 = 0; count2 < COLUMNS_B; count2++)
{
cout<<(*(dynamicArray_A[count] + count2)) + (*(dynamicArray_B[count] + count2))<<"\t";
}
cout<<endl;
}
for (int i = 0; i < ROWS_A; i++)
{
delete []dynamicArray_A[i];
}
delete[]dynamicArray_A;//deletes array A
cout<<endl;
for (int h = 0; h < ROWS_B; h++)
{
delete []dynamicArray_B[h];
}
delete[]dynamicArray_B;//deletes array B
cout<<endl;
};
int mathvar::array_rest()
{
cout<<"Enter values for Array A"<<endl;
cout<<"Enter a number for ROWS: ";
cin>>ROWS_A;
cout<<"Enter a number for COLUMNS: ";
cin>>COLUMNS_A;
int **dynamicArray_A = newint*[ROWS_A];
int **dynamicArray_B = newint*[ROWS_B];
for (i = 0; i < ROWS_A; i++)
{
dynamicArray_A[i] = newint[COLUMNS_A];
cout<<"Enter elements for row number "<<i+1<<": "<<endl;
for (j = 0; j < COLUMNS_A; j++)
{
cout<<"Number "<<j+1<<": ";
cin>>*(dynamicArray_A[i] + j);
}
}//end for for dynamic memory alloc for array A
cout<<"Enter values for Array B"<<endl;
cout<<"Enter a number for ROWS: ";
cin>>ROWS_B;
cout<<"Enter a number for COLUMNS: ";
cin>>COLUMNS_B;
for (h = 0; h < ROWS_B; h++)
{
dynamicArray_B[h] = newint[COLUMNS_B];
cout<<"Enter elements for row number "<<h+1<<": "<<endl;
for (k = 0; k < COLUMNS_B; k++)
{
cout<<"Number "<<k+1<<": ";
cin>>*(dynamicArray_B[h] + k);
}
}//end for for dynamic memory alloc for array B
cout<<"Matrix A"<<endl;
for (i = 0; i < ROWS_A; i++)//prints Array A
{
for (j = 0; j < COLUMNS_A; j++)
{
cout<<*(dynamicArray_A[i] + j)<<"\t";
}
cout<<endl;//forces to next line
}//end of print array A
cout<<endl;
cout<<" + "<<endl;
cout<<"Matrix B"<<endl;
for (i = 0; i < ROWS_B; i++)//prints Array B
{
for (j = 0; j < COLUMNS_B; j++)
{
cout<<*(dynamicArray_B[i] + j)<<"\t";
}
cout<<endl;//forces to next line
}//end of print array B
cout<<" = "<<endl;
for (int count = 0; count < ROWS_B; count++)
{
for (int count2 = 0; count2 < COLUMNS_B; count2++)
{
cout<<(*(dynamicArray_A[count] + count2)) - (*(dynamicArray_B[count] + count2))<<"\t";
}
cout<<endl;
}
for (int i = 0; i < ROWS_A; i++)
{
delete []dynamicArray_A[i];
}
delete[]dynamicArray_A;//deletes array A
cout<<endl;
for (int h = 0; h < ROWS_B; h++)
{
delete []dynamicArray_B[h];
}
delete[]dynamicArray_B;//deletes array B
cout<<endl;
};
char mathvar::menu()
{
cout<<"Matrix Calculator"<<endl;
cout<<"A. Sum"<<endl;
cout<<"B. Substract"<<endl;
cin.get(choice);
cin.ignore();
if (cin.fail())
{
cout<<"ERROR...program will now terminate..."<<endl;
return -1;
}//end if fail
else
{
switch(toupper(choice))
{
case'A':cout<<"You selected sum: "<<endl<<endl;
array_sum();
break;
case'B':cout<<"You selected Substract: "<<endl<<endl;
array_rest();
break;
default: cout<<"You did not enter a valid option..."<<endl;
}//end switch
}//end else
};
Strange enough, this code does compiles, generates an .exe fila and runs smoothly in C-Free 4.0 so I don't know what is the problem with Visual Studio 2010.
I already tried searching the forums but can't find an answer to why my code won't run in Visual Studio 2010...
this is just a suggestion.. look at line 57 in yer code up there ^
int **dynamicArray_B = newint*[ROWS_B];
well it may happen somewhere else but it's possible you're using a variable which is declared but not assigned a value, as a delimiter.. I could be wrong but.. maybe get rid of your constructor:
1 2 3 4 5 6 7
mathvar::mathvar()
{
int ROWS_A = 0;
int COLUMNS_A = 0;
int ROWS_B = 0;
int COLUMNS_B = 0;
}
and let it use the default constructor, and see if the same problem occurs?
I am just as curious as you are to the problem. Also curious, when you compile this in C-Free and run it, does it actually work the way you want?
mathvar::mathvar()
{
int ROWS_A = 0;
int COLUMNS_A = 0;
int ROWS_B = 0;
int COLUMNS_B = 0;
}
This does not initialize the member variables, it creates new variables inside this constructor function which have no relation to the ones one lines 8-14.
Also, you should not make these names in ALL_CAPS unless they are constant - in your code they can change and are not constant, so having their names in ALL_CAPS is misleading as conventionally an ALL_CAPS variable name means it is constant.