Validating sudoku columns and rows I'm writing a sudoku col/row validator, what is does is:


Reads user input on how many Sudoku instances to validate.
Reads the sudoku matrix grid 9x9 (for me, in my specific case 9x9) into a 2d array. Each line read/row should contain something like: 8 2 1 3 6 4 9 7 5.
Validates each row and column by flipping a 9 bit bitset to test whether all columns and rows contains nums from 1-9.


Please note that the only purpose of this program is to validate sudoku columns/rows as specified and not to completely validate it. i.e also checking each sudoku block.

How can I improve my code? I believe there's lots to it, naming conventions, fundamental approach...etc..etc.

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;bitset&gt;

namespace Constants
{
    constexpr int ROW_COL_SIZE = 9;
    const std::string STR_YES = "YES";
    const std::string STR_NO = "NO";
}

template&lt;unsigned int N&gt;
class Sudoku
{
private:
    int m_matrix[N][N] {{0}};
public:
    void ReadRows();
    std::string strIsValid() const;
};

template&lt;unsigned int N&gt;void Sudoku&lt;N&gt;::ReadRows()
{
    for(unsigned int row{0}; row&lt;N; row++)
    {
        static std::string line;
        std::getline(std::cin, line);
        std::istringstream issline(line);
        int readnum {0};
        for(unsigned int i{0}; i&lt;N; i++)
        {
            issline &gt;&gt; readnum;
            m_matrix[row][i] = readnum;
        }
    }
}

template&lt;unsigned int N&gt;std::string Sudoku&lt;N&gt;::strIsValid() const
{
    //9bit default ctor all zeroes, 000000000
    std::bitset&lt;N&gt; bitRow[N];
    std::bitset&lt;N&gt; bitCol[N];
    for(int i{0}; i&lt;N; i++)
    {
        for(int j{0}; j&lt;N; j++)
        {
            bitRow[i].flip(m_matrix[i][j]-1); //verify nums 1-9 row, bitset index is 0 not 1.
            bitCol[i].flip(m_matrix[j][i]-1); //verify nums 1-9 col
        }
        if(!bitRow[i].all() &amp;&amp; !bitCol[i].all())
        {
            return Constants::STR_NO;
        }
    }
    return Constants::STR_YES;
}

int main(int, char**)
{
    int instances {0};
    std::cin &gt;&gt; instances;
    std::cin.clear();
    std::cin.ignore();

    for(int i{0}; i&lt;instances; i++)
    {
        std::cout &lt;&lt; "Instance " &lt;&lt; i+1 &lt;&lt; std::endl;
        Sudoku&lt;Constants::ROW_COL_SIZE&gt; sudokuInstance;
        sudokuInstance.ReadRows();
        std::cout &lt;&lt; sudokuInstance.strIsValid() &lt;&lt; std::endl &lt;&lt; std::endl;
    }

    return 0;
}
It is not a complete answer. Just some of my thoughts about this code.

C++


Do not compare integers of different signedness. Loop counters i and j in function strIsValid are declared as int, but they are compared with N which is unsigned int. It could lead to some problems. Make the counters unsigned.
Use strict compilation flags. The compiler would tell you about your signed/unsigned issue if you pass it -Wall key.

You should always achieve that the compiler will not display any diagnostic messages with the most strict compilation keys (-Wall, -Wextra, -pedantic, etc).

But you probably would not want to use Clang's -Weverything.
What happened with your main? Your main function looks very unnatural:


int main(int, char**)



If you do not need argc and argv, write just

int main()

About std::endl. You should avoid globally using std::endl. It is not the same as just \n. There is a good answer on SO about this topic.

And why are you printing std::endl twice?


std::cout &lt;&lt; sudokuInstance.strIsValid() &lt;&lt; std::endl &lt;&lt; std::endl;


You do not have to explicitly return from main. You don't have to explicitly return 0; at the end of main.


Programming


Minimize the scope of local variables. You should always minimize the scope of local variables. In your case, for example, scope of the readnum variable can be reduced. Some reading about this topic.
The predicative function should return boolean. Your strIsValid is a predicative function. It means that this function checks something and returns either true or false. But you return string "YES" (true) or "NO" (false). You should return bool instead.
What the strIsValid function does? The name of this function doesn't answer the question.


Architecture


Validate user's input. At this time you do not validate user's input in ReadRows. What if I'll input a number which is greater than 9? Or less than 0? If I'll input too many numbers?..

In this case, you store invalid data into m_matrix. Then this invalid data will be used as arguments of std::bitset's flip() which will cause std::out_of_range.
Split row and cols validating and reading user's input. The Sudoku class should know nothing about reading user's input. The only thing that it should do is validate Sudoku columns and rows! This is called the Single Responsibility Principle (SRP).