Home > Software design >  Repetition of a printf()
Repetition of a printf()

Time:05-04

I'm trying to create a Takuzu 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 !

CodePudding user response:

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.

  • Related