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
  • 7,617
  • 8
  • 64
  • 93
  • http://www.cplusplus.com/reference/cstdio/scanf/ You should handle error situation (your "if(scanf("%d", &input)==1)" does not have "else"). – TT_ Dec 07 '13 at 15:31
  • I've already tried this, it doesn't work, gives exactly the same result. (I tried else continue;) – user2750466 Dec 07 '13 at 15:36
  • What do you mean "exactly the same"? If you put, say, break there, it can't be the same. – TT_ Dec 07 '13 at 15:39
  • Please format your code and provide a minimal example (main function, expected input, expected output). Currently your indents do not match the braces, so I think your braces might be wrong. – jmiserez Dec 07 '13 at 15:39
  • http://stackoverflow.com/questions/17497111/reading-character-with-scanf?rq=1 – TT_ Dec 07 '13 at 16:05

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,130
  • 2
  • 25
  • 38
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
  • 14,462
  • 4
  • 48
  • 70