0

I need a help with the following function, it's expecting an integer input; and when I insert something like "F" (non-numerical characters ), the program gets stuck, it doesn't show any output or let me insert more inputs. how can this be fixed?

int input_legality(int game_board[FIELD_ROWS][FIELD_COLS])
{
    int input=0;
    while(1)
    {
        if(scanf("%d", &input)==1)
        {
            if(input==DOWN || input==LEFT || input==RIGHT || input==UP)
            {
                return input;
            }
            else
                if(input==EXIT)
                {
                    printf("\n program exited by user \n");
                    return 1;
                }
                else
                    if(input==PRINT)
                    {
                        printField(game_board);
                        continue;
                    }
                    else
                    {
                        fprintf(stderr,"your step is undefined, please try another one\n");
                        continue;
                    }
        }
    }  
    return 0;
}
parsley72
  • 8,449
  • 8
  • 65
  • 98

2 Answers2

0

It seems that "F" is left in stdin if scanf() does not read an integer. One answer would be to scan a string if an integer is not detected...

Try to add something like

char bla[256];
scanf("%s",bla);

At the end of the while loop (or in case scanf("%d) failed)

Here is a basic "main" code :

#include <stdio.h>

#define DOWN 0
#define UP 1
#define LEFT 2
#define RIGHT 3

#define EXIT 4
#define PRINT 5
int input_legality()
{
    int input=0;
    while(1)
    {
        if(scanf("%d", &input)==1)
        {


            if(input==DOWN || input==LEFT || input==RIGHT || input==UP)
            {
                return input;
            }

            else{
                if(input==EXIT)
                {
                    printf("\n program exited by user \n");
                    return 1;
                }
                else{
                    if(input==PRINT)
                    {
                        printf("ble %d \n",input);
                        continue;
                    }
                    else
                    {
                        fprintf(stderr,"your step is undefined, please try another one\n");
                        continue;
                    }
                }
            }
        }
        //while(getchar() != EOF); 

        //fflush(stdin);
        char bla[256];
        scanf("%s", bla);

    }  
    return 0;
}

int main ( int argc , char * argv [] )
{
    int i;
    for(i=0;i<42;i++){
        input_legality();
    }
    return 0;
}

This scanf is the easy way : some other may be better. Using switch-case may clarify the code. Bye,

Francis

francis
  • 9,525
  • 2
  • 25
  • 41
0

Always, always, always get (interactive) input a line at a time. That's how the user conceives it, so the program logic should be similar.

The POSIX getline function is very useful. Alternatively, you can use fgets and deal with overlong lines yourself (perhaps implementing getline, since it is pretty easy).

Once you've fetched a line of input, parse it (being sure to error on trailing garbage) using any method: sscanf, strtok_r, strtou?ll?, ...

o11c
  • 15,265
  • 4
  • 50
  • 75