I am trying to close this file so that it stops reading the array twice. i have placed it in different places but i does not seem to work, please look at my code and advise me on were to place the infile.close() Thanks in advance

#include <iostream>					
#include <fstream>					// FILE I/O
#include <stdlib.h>					// STANDARD 
#include <iomanip>					

using namespace std;

//
// PROTOTYPE STATEMENTS
//
void function_name();

//
// GLOBAL VARIABLE DECLARATIONS
//
const int SIZE = 10;
int sum = 0;

int main() 
{
    int two_di_arr[ SIZE ][ SIZE ];
    ifstream infile("Two_Dimensional_Data_In.txt");
    
    for( int row = 0; row < SIZE; row++ )
    {
        int sum = 0;
    		 for( int col = 0; col < SIZE; col++ )
    		 {
		  	 infile >> two_di_arr[ row ][ col ];
             sum += two_di_arr[ row ][ col ];
            	
  		      cout << setw( 6 ) << two_di_arr[ row ][ col ] << " ";	
  		      
           }
        
           cout << sum;
           cout << endl;
       }   
       infile.close();
       //////////////////////////////////////////////////////////////////
       for( int row = 0; row < SIZE; row++ )
    {
       
    		 for( int col = 0; col < SIZE; col++ )
    		 {
                   int sum = 0;
		  	 infile >> two_di_arr[ row ][ col ];
             sum += two_di_arr[ col][ row];
            	
  		      cout << setw( 6 ) << two_di_arr[ row ][ col ] << " ";	
  		      
           }
        
           cout << sum;
           cout << endl;
       }   
       
       
       
       
           

    system("pause");
    return 0;
}
// END MAIN() //////////////////////////////

// FUNCTION DEFINITIONS

void function_name()
{

}

<< moderator edit: added [code][/code] tags >>

Why are you trying to do the same thing twice?

How about doing some if(file.is_open) { /* ... */ } around the second attempt?

actually, I am tying to sum of the rows and then sum up the columns, I can get the rows to sum, but not the columns. I keep changing the variables, but I can't get it to work, thank you for your advice

  1. Take input of the entire 2D array, then calculate the desired values.
  2. Close the file, then reopen it.
  3. Rewind the file to the beginning before taking 2nd input.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.