tags:

views:

104

answers:

1

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;
    }
}
A: 

It's hard to know without seeing the input, but here's a couple of possible issues:

(1) The way you detect that the column read failed is by examining the value of y - but are you sure that it's set to a value outside the range 1-9 by the calling code? Otherwise even if the read fails you might think it succeeds. You could fix this by adding a y=0; to the beginning of the function.

Some more explanation: when you use cin >> y to read an integer into y but there isn't an integer to read (could be because there's a non-digit in the stream, or EOF is reached, or whatever) the value of y won't be touched. So, imagine your input looks like "CB". Then after you cin >> row >> y, row contains 'C' and y still contains whatever value it had before the function was called (remember y is a reference to a variable outside this function!) This is the problem: you check to see if the read succeeded by looking at the value of y, but it's quite possible that it is between 1 and 9 even if the read failed (especially if you're calling this function in a loop and reusing the same y). So your code thinks everything went OK, even though it didn't and the stream has now got its failbit set... it's not surprising if everything goes a bit crazy after this point.

(2) The way you're reading data using extraction from cin could misbehave if a line is incomplete. That's because the reading of an integer will skip leading whitespace - including newlines. So if the (invalid) input line is just "B", for example, you'd read the "B" correctly by extracting a char, and then the integer extraction would swallow the newline and fail (assuming the next input line doesn't start with an integer). Then your ignore would completely skip the next line (unless you don't pick up on the failure because of point (1)!).

Mike Dinsdale
I'm just having a hard time figuring out where it ends up failing. I know that somewhere where it's supposed to give me an "invalid column" my code returns true, and from there on the rest of the calls they use are incorrect.As far as (1), if i understand your question, the range, whether 1-9 or outside of it, is from a function call, no where else.as far as 2, yes that may be it. i will look into that.
codefail
I've tried to add some more explanation to point (1) - does it make more sense? Let me know it it's still unclear ;)
Mike Dinsdale
i was just told that all input will be on the same line, so (2) is not an issue..
codefail
professor told me that students are failing the tests because of clearInput not clearing, but from the looks of it all, seems like it's working? i can't think of a situation where the prior input didn't clear :/
codefail
When you say all the input will be on the same line, do you mean literally that there's no newlines in the input at all, like "C2 5 D1 8 I3 3"? If that's true then using `cin.ignore(std::numeric_limits<streamsize>::max(), '\n')` will dump all the rest of your input, which would be bad. Can you show some of the actual input?
Mike Dinsdale
So all of these functions I have basically call each other in a certain way, but a call to getCoords would be something like:A 9.Like I said, my function works correctly for majority of the test cases, and somewhere near the bottom fails because rather than reading in an incorrect column, it outputs a valid call..
codefail
Also, have you tried adding `y=0` into your function to check if (1) is (part of) the problem? The symptons seem to match up quite well! (including the fact that most tests pass, then an incorrect column isn't recognised and the function returns `true`, then everything else is wrong...)
Mike Dinsdale
i now added y=0; we are given 2 chances to submit every day so i'm trying to setup test cases to test my own function before i submit :/
codefail