outfile problem

This is a discussion on outfile problem within the C++ Programming forums, part of the General Programming Boards category; I'm taking a intro to programming class. I've got a program that has 4 functions that need to write a ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    5

    outfile problem

    I'm taking a intro to programming class. I've got a program that has 4 functions that need to write a statement in the outfile. I can't recall ofstream in each function, so I've been told to either declaring and initializing ofstream as a global variable, or pass it in each function. When I do either though, I get a crapload of errors.

    Anyone know what I might be doing wrong? Or is there a better way to do this?

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    are you passing it by "reference"?

    also, show some code...
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    Yes, it would have to be passed as a reference. I don't know the syntax for it if I was going to do it that way.

    Here's some code. I'm not finished with it yet; I wanted to fix this problem first. Sorry in advance for the length.

    I have the outfile declared in the "printSudoku" function right now. But that's what needs to be changed.

    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    using namespace std;
    #include <string>
    
    void inFile (int A[9][9]);
    void printSudoku (int A[9][9], int row[9], int col[9], int box[3][3]);
    int rowCheck(int A[9][9], int row[9]);
    int colCheck(int A[9][9], int col[9]);
    //int boxCheck(int A[9][9], int box[3][3]);
    
    void main ()
    {
    	int A[9][9], row[9], col[9], box[3][3];	
    	inFile(A);
    	printSudoku(A, row, col, box);
    	system("pause");
    }
    
    void inFile (int A[9][9])
    {	
    	ifstream inData;
    	inData.open("sudoku.txt");
    
    	int i, j;	
    	
    	for (i = 0; i < 9; i++)
    		for (j = 0; j < 9; j++)
    			inData>>A[i][j];
    }
    
    void printSudoku (int A[9][9], int row[9], int col[9], int box[3][3])
    {
    	int i, j;
    	ofstream outData;
    	outData.open("out.txt");
    
    	for (i = 0; i < 9; i++)
    	{
    		if ((i &#37; 3 == 0) && (i != 0))
    		{
    			cout<<endl;
    			outData<<endl;
    		}
    		for (j = 0; j < 9; j++)
    		{
    			if ((j % 3 == 0) && (j !=0))
    			{
    				cout<<"  ";
    				outData<<"  ";
    			}
    			cout<<A[i][j]<<" ";
    			outData<<A[i][j]<<" ";
    		}
    		cout<<endl;
    		outData<<endl;
    	}
    	cout<<endl;
    	outData<<endl;
    
    	rowCheck(A, row);
    	colCheck(A, col);
    	//boxCheck(A, box);
    
    	if ((rowCheck(A, row)) && (colCheck(A, col)) /*&& (boxCheck(A, box))*/)
    		cout<<"This is a valid sudoku solution!"<<endl<<endl;
    	}
    int rowCheck(int A[9][9], int row[9])
    {
    	int i, j, k, l;
    	int v[9][10] = {0};
    	bool present[9][10] = {0};
    	bool b = true;
    
    		for (i = 0; i < 9; i++)
    		{
    		
    			for (j = 0; j < 9; j++)
    			{
    				row[j] = A[i][j];
    				for(k=1;k<10;k++)
    				{
    				
    					if (row[j] == k)
    					{
    						v[i][k]++;
    						present[i][k]= true;
    					}
    					if (v[i][k] > 1)
    					{	
    						cout<<"Inconsistancy detected on row "<<i+1<<" where the number "<<row[j]<<" is repeated on column "<<j+1<<endl;
    						outData<<"Inconsistancy detected on row "<<i+1<<" where the number "<<row[j]<<" is repeated on column "<<j+1<<endl;
    						b = false;
    						v[i][k] = 0;	
    					}
    				}	
    			}
    
    
    			for (l = 1; l <= 9; l++)
    			{
    				if (present[i][l] == false)
    				{
    					cout<<"Inconsistancy detected on row "<<i+1<<" where the following number is missing: "<<l<<endl<<endl;
    					outData<<"Inconsistancy detected on row "<<i+1<<" where the following number is missing: "<<l<<endl<<endl;
    				}
    			}
    		}
    		return b;
    }
    
    int colCheck(int A[9][9], int col[9])
    {
    	int i, j, k, l;
    	int v[9][10] = {0};
    	bool present[9][10] = {0};
    	bool b = true;
    	for (i = 0; i < 9; i++)
    		{		
    			for (j = 0; j < 9; j++)
    			{
    				col[j] = A[j][i];
    				for(k=1;k<10;k++)
    				{
    				
    					if (col[j] == k)
    					{
    						v[i][k]++;
    						present[i][k]= true;
    					}
    					if (v[i][k] > 1)
    					{	
    						cout<<"Inconsistancy detected on column "<<i+1<<" where the number "<<col[j]<<" is repeated on row "<<j+1<<endl;
    						outData<<"Inconsistancy detected on column "<<i+1<<" where the number "<<col[j]<<" is repeated on row "<<j+1<<endl;
    						b=false;
    						v[i][k] = 0;	
    					}
    				}				
    			}
    			for (l = 1; l <= 9; l++)
    			{
    				if (present[i][l] == false)
    				{
    					cout<<"Inconsistancy detected on column "<<i+1<<" where the following number is missing: "<<l<<endl<<endl;
    					outData<<"Inconsistancy detected on column "<<i+1<<" where the following number is missing: "<<l<<endl<<endl;
    				}
    			}
    	
    		}	
    	return b;
    }
    
    /*int boxCheck(int A[9][9], int box[3][3])
    {
    	int i, j, k, l;
    	int v[9][10] = {0};
    	bool present[9][10] = {0};
    	bool b = true;
    
    	int box1[3][3] = {0};
    	int box2[3][3] = {0};
    	int box3[3][3] = {0};
    	int box4[3][3] = {0};
    	int box5[3][3] = {0};
    	int box6[3][3] = {0};
    	int box7[3][3] = {0};
    	int box8[3][3] = {0};
    	int box9[3][3] = {0};
    	
    	for (i=0; i < 3; i++)
    	{
    		for (j=0; j < 3; j++)
    		{
    			box1[i][j]=A[i][j];
    			box2[i][j]=A[i][j+3];
    			box3[i][j]=A[i][j+6];
    			box4[i][j]=A[i+3][j];
    			box5[i][j]=A[i+3][j+3];
    			box6[i][j]=A[i+3][j+6];
    			box7[i][j]=A[i+6][j];
    			box8[i][j]=A[i+6][j+3];
    			box9[i][j]=A[i+6][j+6];
    
    
    		}
    	}
    
    	return b;
    
    }*/

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Okay, several things:

    1. void main is no good. should be:
    Code:
    int main(){
    
      // Your code here
    
      return 0; // This also needs to be here
    }
    2. You haven't passed the ofstream object to any of your functions...

    3. The structure of this program is just whacky.

    4. Don't say "using namespace std;" before any other include statements (i.e. before your #include <string>).

    Consider revising the structure of your program to something like this:
    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include <string>
    
    // No "using" before any "include"
    
    using namespace std;
    
    void function1( std::ofstream& out ){ // pass by reference "&"
      out << "Here's some text";
    }
    
    void function2( std::ofstream& out ){
      out << " and here's some more.";
    }
    
    int main(){ // Must be an "int"
    
      std::ofstream fout("sample.txt"); // Declare your ofstream object in main, no where else
      function1( fout );
      function2( fout );
      
      return 0; // Must have this
    }
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    thanks for the help. it worked. I appreciate it

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Cool. But please do consider making some major revisions to the structure of your program.

    for things that are used in multiple functions, declare them in main, and pass them to your functions (this goes for both ofstream and ifstream objects in your case).

    also, get rid of that system("pause") My guess is that you're using Dev-C++, no?

    use this instead:
    Code:
    std::cout << "Press [Enter] to continue...";
    std::cin.ignore( 256, '\n' );
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21