Error when inserting an invalid input

The expected input is : SET_MINE X Y, while X and Y have to be numbers between 0-19; when the user finishes inserting his data he is expected to write SET_DONE. I'm facing two problems here, which apparently happen due to the same reason: 1. when I insert : SET_MINE (and then press ENTER) or SET_MINE X (inserting only one number rather than two), the line "Insert failed: not enough parameters" is printed on the screen an infinite number of times! 2. The exact same thing happens when I insert numbers out of the range [0,19], e.g: SET_MINE 56 7.Here I'm expecting to see this line on the screen "Insert failed: illegal row/col", but instead I get this one: "Insert failed: not enough parameters" printed an infinite number of times.

Here is my code:

int main()
{
int game_board[FIELD_ROWS][FIELD_COLS]={0};
char szLine[MAX_LINE_SIZE];
char* delimiters = " \t\n";
char* pszCommand;
char* pszCol;
char* pszRow;

BOOL gameContinue= TRUE;
MINE* pHead = NULL;
POSITION pos;

int stepDirection;
int row=0;
int col=0;
int old_row=row, old_col=col, mine=0;

fgets(szLine,MAX_LINE_SIZE,stdin);      // get line from standard input
while (strncmp(szLine,"SET_DONE",8) != 0)
{
    pszCommand = strtok(szLine, delimiters);

    if (NULL == pszCommand )
    {
        continue;
    }
    if (0 == strcmp(pszCommand, "SET_MINE"))
    {
        pszRow = strtok(NULL, delimiters);
        pszCol = strtok(NULL, delimiters);
        if (NULL == pszCol || NULL == pszRow)
        {
            fprintf(stderr, "Insert failed: not enough parameters\n");
            continue;
        }
        row = atoi(pszRow);
        col = atoi(pszCol);
        if (row <0 || row >= FIELD_ROWS || col < 0 || col >= FIELD_COLS)
        {
            fprintf(stderr, "Insert failed: illegal row/col \n");
            continue;
        }

            game_board[row][col]=-1; // a mine is inserted to this place
    }

    fgets(szLine,MAX_LINE_SIZE,stdin); // get line from standard input
}

Any ideas?!

Solutions

This is because you are using continue so your second fgets is never executed. See the standard: http://msdn.microsoft.com/en-us/library/6e3dc2z3.aspx (Microsoft's site, but still applicable)

Think of the continue statement like telling the loop "Okay, this iteration is done, start the next one."

So when you reach the part where you tell the user they didn't supply enough parameters, this is all that the loop sees:

fgets(szLine,MAX_LINE_SIZE,stdin);      // get line from standard input
while (strncmp(szLine,"SET_DONE",8) != 0)
{
   ...;
   if (0 == strcmp(pszCommand, "SET_MINE"))
   {
        pszRow = strtok(NULL, delimiters);
        pszCol = strtok(NULL, delimiters);
        if (NULL == pszCol || NULL == pszRow)
        {
            fprintf(stderr, "Insert failed: not enough parameters\n");
            continue;
        }
   }
   //nothing down here
}

In the eyes of the program, the second fgets at the bottom of the loop doesn't exist.

You can fix this by using more clever conditional logic.

This (untested) code snippet should fix things:

while (strncmp(szLine,"SET_DONE",8) != 0)
{
    pszCommand = strtok(szLine, delimiters);

    if (pszCommand != NULL && 0 == strcmp(pszCommand, "SET_MINE"))
    {
        pszRow = strtok(NULL, delimiters);
        pszCol = strtok(NULL, delimiters);
        if (NULL != pszCol && NULL != pszRow)
        {
            row = atoi(pszRow);
            col = atoi(pszCol);
            if (row < 0 || row >= FIELD_ROWS || col < 0 || col >= FIELD_COLS)
            {
               fprintf(stderr, "Insert failed: illegal row/col \n");
            }
            else
            {
               game_board[row][col]=-1; // a mine is inserted to this place
            }
        }
        else{
            fprintf(stderr, "Insert failed: not enough parameters\n");
        }

    }

    fgets(szLine,MAX_LINE_SIZE,stdin); // get line from standard input
}
Tags: C / Visual C++ / Null

Similar questions

Input-dependent error: sendto() error code 22 (Invalid argument) depending on input size
I'm having issues implementing a C UDP socket program. The code below works perfectly with any input shorter than 56 characters, but if I feed it 56 characters or more, sendto complains that I gave it invalid arguments (error code 22). For instance, this will send correctly: But this won't: What gives? This was the version of the code I was actuall...
Error in './a.out': free(): invalid*** Error in `./allum1': free(): invalid next size (fast): 0x00000000023e13f0 ***
I'm working on a little game in C : A board like this is displayed : And you have to take sticks and make the AI lose by letting it pick the LAST stick. Wanting to give the player the ability to chose the size of the board I have coded the following function : Please note that my_strdup and my_strcat are exactly like strdup and strcat. But here is ...
C
error while inserting data in an array- invalid types 'int[int]' for array subscript
I am getting this error when trying to compile my program the code part I am getting this error is: Complete code can be viewed here. I have googled for this and found several questions with the same but cant figure out whats the problem.
If i input too many characters my program either has a 'mremap_chunk(): invalid pointer' or a 'realloc(): invalid old size'
my program was built as a test to input as many sentences as the user want (until he enters -1), and then concatenate all sentences (\n included). If i input some characters is fine, but if i input more then 25 characters i have the two errors listed above, i tried simulating what would happen in paper and i canĀ“t find the problem, help is apprecia...
C
C program to find the number of elements in an array after inserting and without counting while inserting
Looking for a C program to count the number of elements in an integer array after inserting and without counting while inserting.Alternatively, what is the integer array substitution for strlen() ? In this program I need to determine the value of c:

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.