1

I'm trying to create a game. In order to do it, I have two functions size() and ask_column().

Size() ask for the size of the grid wanted by the user. It return the number that should be 4 or 8.

int size() /*Ask for the size of the Takuzu that the user wants*/
{
    int size;
    do {
        printf("Type the size of Takuzu that you want (only 4*4 and 8*8 are available): \n");
        scanf("%d",&size);
    }while(size != 4 && size != 8);

    return(size);
}

Ask_column() ask for the column desired by the user. You enter the size of the grid in the matrix and it return a character that should be A to D if 4 4 grid and A to H if 8 8 grid.

char ask_column(int s) /*Ask the user in which column he wants to put his value*/
{
    char column;
    if (s==4)
    {
        do { /*Ask for the column if 4*4*/
            printf("Enter the column of the value you want to enter (A to D): \n");
            scanf("%c", &column);
        } while (column != 'A' && column != 'B' && column != 'C' && column != 'D' && column != 'a' && column != 'b' &&
                 column != 'c' && column != 'd');
    }
    else
    {
        do { /*Ask for the column if 8*8*/
            printf("Enter the column of the value you want to enter (A to H): \n");
            scanf("%c", &column);
        } while (column != 'A' && column != 'B' && column != 'C' && column != 'D' && column != 'E' && column != 'F' &&
                 column != 'G' && column != 'H' && column != 'a' && column != 'b' && column != 'c' && column != 'd' && column != 'e' && column != 'f' &&
                 column != 'g' && column != 'h');
    }
    return column;
}

The main issue I'm having is a repetition of the question from the Ask_column(). If size() is used, ask_column() will always ask the question 2 times. If not only one which is what I want.

int main()
{
    int s;
    s = size();
    ask_column(s);
}

Return :

Type the size of Takuzu that you want (only 44 and 88 are available): 4 Enter the column of the value you want to enter (A to D): Enter the column of the value you want to enter (A to D):

int main()
{
    int s;
    s = 4;
    ask_column(s);
}

Return :

Enter the column of the value you want to enter (A to D):

I really would like to know were this repetition is coming from.

Thanks to all the persons that would try to help !

  • 1
    Hint: `while (column != 'A' && ...)` -> `while (!strchr("ABCDabcd", column))` – tstanisl May 04 '22 at 12:07
  • If you are entering data interactively, and you enter `4` and then `A`, how many keys do you hit? – William Pursell May 04 '22 at 12:12
  • What do you mean by key ? As those functions are part of a bigger project, if I enter 4 and A then I call other functions to play to Takuzu. – Antoine Dupont May 04 '22 at 12:16
  • @AntoineDupont You type `4`, and then ``, and then `A`. What does your program do with the `` key? – William Pursell May 04 '22 at 12:18
  • @WilliamPursell After entering that it will convert this chararacter to a value. This value is used with a `row` value and a `value` to be inserted in the grid. – Antoine Dupont May 04 '22 at 12:26
  • @AntoineDupont That is what your program does with the `4`. Then it prints a prompt asking for the column and it reads the newline. The newline is not a valid column, so it prints the prompt again. The input stream is `4\nA`, and you are forgetting about the newline. – William Pursell May 04 '22 at 12:30

1 Answers1

0

You really shouldn't use scanf for user input. In this case, the solution is pretty simple: you need to consume whitespace. After the user enters the first number, there is a newline character remaining to be read from the input stream. The scanf in ask_column reads it and sees that it is not one of the desired entries, so the prompt is written again. To avoid that problem, the simple solution is to add some whitespace in the conversions specifier:

scanf(" %c", &column);

But this is not a good solution. Consider the behavior of your program when someone enters W for the size. The program will immediately go into an infinite loop, continually attempting to read an integer but always stopping when it sees that W is not a valid character in an int. You must add some input validation and consume invalid data. scanf is simply not the best tool for this, and your life will be much easier in the long run if you stop using it now.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Oh thank you very much. As this project is for a school purpose, we only learn to use `scanf` for now. But I understand what you mean and will try to correct this. – Antoine Dupont May 04 '22 at 12:21