Writing and Reading text files

closed account (1TpXSL3A)
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.

Please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 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();


Thanks.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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(); 
closed account (1TpXSL3A)
Thanks, I've got it working.
I'm also having trouble writing and reading the elements of matrices from a text file.

The code I have is


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "Matrix A" << endl;
for ( int i = 0; i < 3; i++ )
      for ( int j = 0; j < 3; j++ )
      {
         cout << "A[" << i+1 << "][" << j+1 << "]: ";
         cout << A[i][j]<< endl;
		
      }
	  cout<< " " << endl;
	  cout <<"Matrix B"  <<endl;

	for ( int i = 0; i < 3; i++ )
      for ( int j = 0; j < 3; j++ )
      {
         cout << "B[" << i+1 << "][" << j+1 << "]: ";
         cout << B[i][j]<< endl;
		
      }                      


This only prints the elements out but i'm not sure how to make it how i need it to be.
Thanks




but i'm not sure how to make it how i need it to be.

Could you clarify what you mean - what are your needs here?
Topic archived. No new replies allowed.