开发者

Still failing a function, not sure why...ideas on test cases to run?

I've been trying to get this Sudoku game working, and I am still failing some of the individual functions. All together the game works, but when I run it through an "autograder", some test cases fail.. Currently I am stuck on the following function, placeValue, failing. I do have the output that I get vs. what the correct one should be, but am confused..what is something going on?

EDIT: I do not know what input/calls they make to the function.

What happens is that "invalid row" is outputted after every placeValue call, and I can't trace why..

Here is the output (mine + correct one) if it's at all helpful: http://pastebin.com/Wd3P3nDA

Here is placeValue, and following is getCoords that placeValue calls..

void placeValue(Square board[BOARD_SIZE][BOARD_SIZE])
{   
    int x,y,value;  

    if(getCoords(x,y))
    { 

        cin>>value;

        if(board[x][y].permanent)
        {
            cout<< endl << "That location cannot be changed";   

        }
        else if(!(value>=1 && value<=9))
        {
            cout << "Invalid number"<< endl;
            clearInput();
        }
        else if(validMove(board, x, y, value))  
        {
            board[x][y].number=value;
        }
    }
}


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

    cin>>row>>y;
    x = static_cast<int>(toupper(row));
   if (isalpha(row) && (x >= 'A' && x <= 'I') && y >= 1 && y <= 9)
   {
      x = x - 'A'; // converts x from a letter to corresponding index in matrix
      y = y - 1;   // converts y to corresponding index in matrix
      return (true);
   }
   else if (!(x >= 'A' && x <= 'I'))
   {
    cout<<"Invalid row"<<endl;  

    clearInput();
    return false;
   }
   else 
   {
    cout<<"Invalid column"<<endl;
    clearInput();
  开发者_如何学Python  return false;
   }

}


(Replicating my comment on the original post) Working blind like this is pointless. The grader shows you your program's output, right? So just echo stdin to stdout, and then paste it here so we can see what you're supposed to be processing: string s; while (getline(cin, s)) cout<<s<<endl;

Meanwhile:

Your clearInput() function is simply reading and ignoring till the end of the line if there's a parse error; if that's how they're doing it, it'd be far easier to just use getline() to read a line and then parse it.

Just for kicks, here's a much more robust version of your getCoords function:

bool getCoords(int& x, int& y, int& value)
{
    string s;
    getline(cin, s);
    istringstream ss(s);
    char a, b, c;
    ss >> a >> b >> c >> ws;
    if (ss.fail() || !ss.eof()) return false;
    a = toupper(a);
    if (a < 'A' || a > 'I') return false;
    x = a - 'A';
    if (b < '1' || b > '9') return false;
    y = b - '1';
    if (c < '1' || c > '9') return false;
    value = c - '1';
    return true;
}
  1. It reads in the input a line at a time, so you don't need to use a silly clearInput() anymore.
  2. It reads in three characters, and three only; any more or any less and it'll skip the line and return false; any problems trying to read and it'll return false. (Not sure if that's how you're supposed to do it, but if you only want perfectly well formed lines, it's the way to go).
  3. It performs all your range validation before returning true, so your outer function doesn't need to worry about it.

Just replace your getCoords(x,y) call with a getCoords(x, y, value), and take out the cin>>value; line.


If it's printing invalid row after every input, we have to conclude that your code is doing what it says: It thinks that every row is outside the range A-I. Most likely some input got your cin into a frozen state and the cin >> row is actually either sticking a 0 or nothing at all into row, and thus it's constantly failing the check and printing the message.


You should probably also fix this line:

cout << endl << "That location cannot be changed"; 

to

cout << "That location cannot be changed" << endl;

A possible fix might be that if the row is invalid, you should NOT read the column. This is a very vague guess though.

This would assume that the input looks something like

A1 9
C3 3
d 9 8
q
A3 9

Your parser would fail at that input... I'm not sure how mean they are being with the input though.

I'm also not sure how much freedom you're being given to implement this, but if you're parsing line by line, it's often better to use getline(), and then parse each string. That would require some refactoring though.


Again, with the really mean input. Does anyone here think that they would give the following input?

A 1 3
A 
A 2 4

In that case, (s)he would really have to use getline() or noskipws and some additional parsing logic that would be fairly mean in a homework setting.


To give some explination on why I'm guessing this, it seems that you are missing lines of output. This would seem to me that in each input reading stage, you might be reading two lines of input, instead of just one.


Got further clarification from the Prof:

If the user types in 'P' and then presses enter, the program is to wait for further input (meaning it is waiting for the row, then the y then the value)

So, I do not need to account for input like

A
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜