Home > Software engineering >  C ostream parameters
C ostream parameters

Time:10-15

I am building a project that solves a boggle board. The two functions I'm interested in at the moment are

void Boggle::SolveBoardHelper(bool printBoard, int row, int column, int rowMax, int columnMax,
                              ofstream& output, int numSteps, int steps[4][4], string currPath)


void Boggle::SolveBoard(bool printBoard, ostream &output) {
    ofstream outputFile;
    outputFile(output); // stuck here
    string currPath; // used to track the current path on the board

    for(int pos_x = 0; pos_x < BOARD_SIZE; pos_x  ){
        for(int pos_y = 0; pos_y < BOARD_SIZE; pos_y  )
            SolveBoardHelper(print_or_not, pos_x, pos_y, BOARD_SIZE, BOARD_SIZE, outputFile, numSteps, visited, currPath);
    }  

}

My area of confusion is that when the user calls SolveBoard to solve a board, they have the option of passing "cout" or an output file into the ostream parameter. If the user decides to call the function and pass in an output file, say "output.txt", how do I account for this? How do I take the output file passed into the SolveBoard parameter by the user, and then plug that into the SolveBoardHelper function so that I can write to that file after the board has been solved?

My previous attempt included me just passing a predetermined output file into the parameter, but I don't know what file the user will choose to output to:

void Boggle::SolveBoard(bool printBoard, ostream &output) {
   
    string outputFile = "solve_output_test.txt"; // write to the already created solve_output_test.txt file
    ofstream outFile(outputFile); // assign it to ofstream variable outFile
    if(!outFile){
        cout << "Error opening file" << endl; // file open check
    }
    string currPath; 
    
    for(int pos_x = 0; pos_x < BOARD_SIZE; pos_x  ){
        for(int pos_y = 0; pos_y < BOARD_SIZE; pos_y  ) 
            SolveBoardHelper(print_or_not, pos_x, pos_y, BOARD_SIZE, BOARD_SIZE, outFile, numSteps, visited, currPath);
    }   
}

CodePudding user response:

void Boggle::SolveBoard(bool printBoard, ostream &output) {
    // remove the two lines below and just use `output` in the rest of the function:
    //ofstream outputFile; 
    //outputFile(output);
    
    for(int pos_x = 0; pos_x < BOARD_SIZE; pos_x  ) {
        for(int pos_y = 0; pos_y < BOARD_SIZE; pos_y  )
            SolveBoardHelper(print_or_not, pos_x, pos_y, BOARD_SIZE, BOARD_SIZE, output,
                             numSteps, visited, currPath);
    }  
}

Then call the function with

SolveBoard(the_bool, std::cout);

or

std::ofstream file("output.txt");
SolveBoard(the_bool, file);

You also need to redefine SolveBoardHelper to take an ostream instead:

void Boggle::SolveBoardHelper(bool printBoard, int row, int column, int rowMax,
                              int columnMax, ostream& output, int numSteps,
                              int steps[4][4], string currPath)
  •  Tags:  
  • c
  • Related