vendredi 24 septembre 2021

C- How to restrict user to only enter a specific amount of elements?

I want the user to enter 10 numbers, and if they enter anything else but 10 numbers I want to tell them it's invalid. Not quite sure how to do it, I tried with an if-statement like this:


{
    char numbers[11];
    printf("Enter your 10 numbers: ");
    scanf("%s", numbers);

    if ( *numbers != 11)
        printf("Enter a valid number.\n");
    else
        printf("Your number is %s: \n", numbers);

    return 0;
}

Is there a way to do this?

Aucun commentaire:

Enregistrer un commentaire