I need to put file into 2D array and then transpose it.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darbear02
    New Member
    • Apr 2014
    • 1

    #1

    I need to put file into 2D array and then transpose it.

    Input File
    Code:
    3 7 2 9 0
    1 8 4 6 5
    0 2 7 4 3
    6 1 9 5 8
    then, input manually 5 digits: 24161
    So, the array will have to be 5x5 in the end. Im a beginner at C++ and having a real tough time.
    Program so far.
    Code:
    #include<iostream> 
    #include<fstream>
    #include<cstdlib>
    #include<string>
    #include<sstream>
    #include<vector>
    using namespace std;
    ifstream inFile;
    int  i, ID = 0, matrix[5][3], j, k;
    
    
    
    
    
    int main()
    {
    	string b = "";
    	string ifilename, line, ofilename;
    	ifstream inFile, checkOutFile;
    	ofstream outFile;
    	char response;
    	// Input file
    	cout << "Please enter the name of the file you wish to open : ";
    	cin >> ifilename;
    	inFile.open(ifilename.c_str());
    	if (inFile.fail())
    	{
    		cout << "The file " << ifilename << " was not successfully opened." << endl;
    		cout << "Please check the path and name of the file. " << endl;
    		exit(1);
    	}
    	else
    	{
    		cout << "The file is successfully opened." << endl;
    	}
    	// Output file
    	cout << "Please enter the name of the file you wish to write : ";
    	cin >> ofilename;
    	checkOutFile.open(ofilename.c_str());
    	if (!checkOutFile.fail())
    	{
    		cout << "A file " << ofilename << " exists.\nDo you want to continue and overwrite it? (y/n) : ";
    		cin >> response;
    		if (tolower(response) == 'n')
    		{
    			cout << "The existing file will not be overwritten. " << endl;
    			exit(1);
    		}
    	}
    	outFile.open(ofilename.c_str());
    	if (outFile.fail())
    	{
    		cout << "The file " << ofilename << " was not successfully opened." << endl;
    		cout << "Please check the path and name of the file. " << endl;
    		exit(1);
    	}
    	else
    	{
    		cout << "The file is successfully opened." << endl;
    	}
    	// Copy file contents from inFile to outFile
    
    	cout << "Please enter the last 5 digits in your student ID : ";
    	cin >> ID;
    	cout << " " << endl;
    	cout << "The numbers from the input file with added last row." << endl;
    	
    	while (getline(inFile, line))
    		cout << line << endl;
    	
    	
    	stringstream ss;
    	if (ID < 0)
    		ID = -ID;
    	ss << ID; //convert to individual numbers
    	ss >> b;
    	for (int i = 0; i < b.length(); ++i)
    	{
    		cout << b[i] << " ";
    	}
    	cout << " " << endl;
    	cout << " " << endl;
    
    	cout << "The transposed numbers." << endl;
    	cout << " " << endl;
    
    
    
    	inFile.close();
    	outFile.close();
    } // main
    Last edited by Rabbit; Apr 22 '14, 03:13 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
Working...