I've written a code where certain operations occur on a set of matrices but i need it so this code is displayed on the screen and written into a text file.
My code below makes it so it's in a file, but then nothing is printing on my screen.
ofstream Operation;
Operation.open ("Operation.txt");
Operation << "The Operation you chose is" << p <<endl;
if (p==1)
{{
cout << "detA is:"<< detA << endl;
cout<< "detB is:" << detB<< endl;
}
if (p==2)
{
cout << "the sum of A and B, C is" <<endl;
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
cout << "A+B["<< i+1 << "]["<< j+1 << "]:" << A[i][j] + B[i][j]<< endl;
}
cout<< " " << endl;
}
if (p==3)
{
cout << "the difference between A and B is" <<endl;
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
cout << "A-B["<< i+1 << "]["<< j+1 << "]:" << A[i][j] - B[i][j]<< endl;
}
}
if (p==4)
{
cout << endl;
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
C =+ A[i][j]*B[i][j];
}
cout << "The Matrix Product is "<< C << endl;
}
else
cout << "This is not an operation" <<endl;
}
Operation.close();
It looks like there's a double opening brace after the first if statement if (p==1) meaning all the remaining code is executed only when p is 1, but then the other if statements will be false.
The indentation isn't very consistent, which makes it less obvious.
ofstream Operation;
Operation.open ("Operation.txt");
Operation << "The Operation you chose is" << p <<endl;
if (p==1)
{
{
cout << "detA is:"<< detA << endl;
cout<< "detB is:" << detB<< endl;
}
if (p==2)
{
cout << "the sum of A and B, C is" <<endl;
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
cout << "A+B["<< i+1 << "]["<< j+1 << "]:" << A[i][j] + B[i][j]<< endl;
}
cout<< " " << endl;
}
if (p==3)
{
cout << "the difference between A and B is" <<endl;
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
cout << "A-B["<< i+1 << "]["<< j+1 << "]:" << A[i][j] - B[i][j]<< endl;
}
}
if (p==4)
{
cout << endl;
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
C =+ A[i][j]*B[i][j];
}
cout << "The Matrix Product is "<< C << endl;
}
else
cout << "This is not an operation" <<endl;
}
Operation.close();