Search code examples
c++arraysgetter-setter

My value is not changing in my set function


The value of n is being passed to my void Cell::setValue(int value) function but m_value does not change. Can anyone explain what is wrong here?

File read in

void SudokuPuzzle::readPuzzle(const char filenameIn[]) {
// Add code to read in a puzzle from the text file and store within the SudokuPuzzle object
ifstream inStream;
inStream.open(filenameIn);


for (int i = 0; i < 9; i++)
{
    for (int j = 0; j < 9; j++)
    {
        int n;
        inStream >> n;
        m_grid[i][j].setValue(n);
    }
}

Set value in cell

void Cell::setValue(int value)
{
    m_value = value;

    if (m_value = 0)
    {
        m_given = true;
    }
}

For example when n is set to 3 it is passed through to value.

m_value = value does not set m_value to 3.


Solution

  • In this condition:

    if (m_value = 0)
    

    even though m_value was previously assigned the value of value, it's assigned the value 0 here.

    You probably meant to compare m_value to 0, like this:

    if (m_value == 0)
    

    If you turn on your compiler warnings (e.g. with -Wall), you'll be told that this code is likely a bug.