Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:33:57+00:00 2026-06-12T17:33:57+00:00

NOTE: skip to bottom for real question Hey guys, right now I am in

  • 0

NOTE: skip to bottom for real question

Hey guys, right now I am in a rut where I know the basics how to solve my sudoku program, but if I did it the way i know how it would be ugly, long and a lot of unnecessary code.

Right I have sudoku.cc, sudokuboard.cc, sudokuboard.h, stack.cc, and stack.h,
So as of right now it doesn’t matter what my stack.cc and my stack.h are because they are rather generic and anyone can find the programs to them and frankly I don’t underdstand why I even need to include the stack.cc/h due.

My sudoku.cc as of right now is:

#include <iostream>
#include <cassert>
#include <fstream>
#include "sudokuboard.h"
#include "stack.h"

using namespace std;
    int main (void)
    {
        FILE *fp = fopen("test.txt","r");
        char sudoku_grid[9][9];
        char ch;

        if(fp != NULL)
        {
            for(int i = 0; i < 9; i++)
            {            
                for(int j = 0; j < 9; j++)
                {
                    ch = fgetc(fp);
                    sudoku_grid[i][j] = ch;   
                }
                ch = fgetc(fp);
            }
            for(int i = 0; i< 9;i++)
            {
                for(int j= 0; j<9;j++)
                    cout<<sudoku_grid[i][j];
                cout<<endl;
            }
        }  
     return 0; 
    }

Which so far does nothing but print out the file I gave it and have it stored in a 2D array.

My sudokuboard.h is

#include <iostream>

#define SDIM 9

class SudokuBoard {
 public:
  //------------------------------------------------------------------------
  SudokuBoard();
  // Construct a blank sudoku board
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  void print(std::ostream & ostr) const;
  // display it.  duh.
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  void place(size_t r, size_t c, char digit);
  // PRE: safe(r,c,digit)
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  void remove(size_t r, size_t c, char digit); 
  // PRE: get(r,c) == digit
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  char get(size_t r, size_t c) const;
  // Return the digit at (r,c) on the board.  or ' ' if blank.
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  bool safe(size_t r, size_t c, char digit) const;
  // 
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  bool done() const; 
  // Return true iff every cell has a number
  //------------------------------------------------------------------------
 private:
  std::string rows[SDIM];
};

and my sudokuboard.cc:

#include <iostream>
#include <cassert>
#include "sudokuboard.h"

#define ASSERTBOUNDS assert(0 <= r and r < SDIM and 0 <= c and c < SDIM)

SudokuBoard::SudokuBoard()
{
  for (size_t i = 0;i<SDIM;i++) {
    rows[i] = "";
    for (size_t j=0;j<SDIM;j++)
      rows[i] += ' ';
  }
}

void SudokuBoard::place(size_t r, size_t c, char digit)
{
  ASSERTBOUNDS;
  assert(safe(r,c,digit));
}

void SudokuBoard::remove(size_t r, size_t c, char digit)
{
  ASSERTBOUNDS;
  assert(get(r,c)==digit);
  rows[r][c] = ' ';
}

char SudokuBoard::get(size_t r, size_t c) const
{
  ASSERTBOUNDS;
  return rows[r][c];
}


void SudokuBoard::print(std::ostream & ostr) const
{
  for (size_t i=0;i<SDIM;i++)
    ostr << rows[i] << std::endl;
}
bool SudokuBoard::safe(size_t r, size_t c, char digit) const
{
 for(size_t r=0; r<SDIM; r++)
    for(size_t c=0; c<SDIM; c++)
       if (get(r,c) == digit)
           return false;

 for(size_t c=0; c<SDIM; c++)
    for(size_t r=0; r<SDIM; r++)
       if (get(r,c) == digit)
           return false;
  return true;
}
bool SudokuBoard::done() const
{
  for (size_t r=0;r<SDIM;r++)
    for (size_t c=0;c<SDIM;c++)
      if (rows[r][c]==' ')
    return false;
  return true;
}

So as of right now I want to use sudokuboard.cc methods and use the SudokuBoard() in my sudoku.cc because I know if I do that I will be able to use the useful methods provided by Sudokuboard.cc thus making my code seem a lot cleaner because it isn’t all cluttered up on one program.

So as of right now I think the gateway to linking these two programs together in order to maximize my space I would have to set my current 2D array which is sudoku_grid to be part of Sudokuboard() some how.

The problem is that I literally have no blinkin idea how to hook this puppies up. I know I cannot do sudoku_grid = Sudokuboard() because even if it did work, it would probably set all the arrays to being just spaces.

Anyone have a solution of where to start off?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:33:58+00:00Added an answer on June 12, 2026 at 5:33 pm

    At the top of the main file put #include "sudokuboard.h" so that the compiler knows it’s there. Next, at the top of your int main() you need to create an instance of the SudokuBoard class, with SudokuBoard mainBoard;, then you can access it’s methods via mainBoard.doSomething(); somewhere else in the main. I’d recommend creating a new method in the SudokuBoard class that reads data from a file and places it directly into SB’s private std::string rows[SDIM]; array from within (basically move most of the code from main into the class). With the code as-is, this should work assuming I didn’t mangle any of the names:

    #include "sudokuboard.h"
    
    int main (void) {
      SudokuBoard mainBoard;
      FILE *fp = fopen("test.txt","r");
      char sudoku_grid[9][9];
      char ch;
    
      if(fp != NULL) {
        for(int i = 0; i < 9; i++) {            
          for(int j = 0; j < 9; j++ ) {
            ch = fgetc(fp);
            //sudoku_grid[i][j] = ch;  
            //set mainBoard's internal state, etc here
            mainBoard.place(i,j,ch);
          }
          ch = fgetc(fp);
        }
        for(int i = 0; i< 9;i++) {
          for(int j= 0; j<9;j++) cout<<sudoku_grid[i][j];
          cout<<endl;
        }
      }  
      return 0; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: To understand my question, you might need to know about my project and
Note : jump down to Question below if you just want to skip the
Note: You may not need to read the whole thing to know what's wrong...
This question is specific to reactive-banana and real-time simulations with a physical and visual
NOTE: I know the various reasons to avoid using the session, but this is
Note: Move this question to other site if you think its not suite here
Kind of theoretical question. Quite long so feel free to skip if you are
Edit : See my full answer at the bottom of this question. tl;dr answer
Question Using iTextSharp, can I skip over the first page of the document, and
this is related to my last question ( NOTE: I already got some good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.