Incorrect logic flow? function that gets coordinates for a sudoku game

Posted by igor on Stack Overflow See other posts from Stack Overflow or by igor
Published on 2010-04-05T16:27:49Z Indexed on 2010/04/05 16:33 UTC
Read the original article Hit count: 261

Filed under:
|

This function of mine keeps on failing an autograder, I am trying to figure out if there is a problem with its logic flow? Any thoughts?

Basically, if the row is wrong, "invalid row" should be printed, and clearInput(); called, and return false. When y is wrong, "invalid column" printed, and clearInput(); called and return false.

When both are wrong, only "invalid row" is to be printed (and still clearInput and return false.

Obviously when row and y are correct, print no error and return true.

My function gets through most of the test cases, but fails towards the end, I'm a little lost as to why.

bool getCoords(int & x, int & y)
{
    char row;
    bool noError=true;

    cin>>row>>y;
    row=toupper(row);

    if(row>='A' && row<='I' && isalpha(row)  && y>=1 && y<=9)
    {   
        x=row-'A';
        y=y-1;  
        return true;
    }

    else if(!(row>='A' && row<='I'))
    {
        cout<<"Invalid row"<<endl;
        noError=false;
        clearInput();
        return false;   
    }

    else
    {
       if(noError)  
       {
           cout<<"Invalid column"<<endl;
       }
       clearInput();
       return false;
    }
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about homework