Comment scanf seulement entier et répétez la lecture si l'utilisateur entre des caractères non numériques?

Ici est un peu jeune tyro de problème avec du code C en essayant simplement d'empêcher l'utilisateur de taper un caractère ou un nombre entier inférieur à 0 ou supérieur à 23 ans.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    const char *input;
    char *iPtr;
    int count = 0;
    int rows;

    printf("Enter an integer: ");
    scanf("%s", input);
    rows = strtol(input, &iPtr, 0);
    while( *iPtr != '
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *input;
char *iPtr;
int count = 0;
int rows;
printf("Enter an integer: ");
scanf("%s", input);
rows = strtol(input, &iPtr, 0);
while( *iPtr != '\0') //Check if any character has been inserted
{
printf("Enter an integer between 1 and 23: ");
scanf("%s", input);
}
while(0 < rows && rows < 24) //check if the user input is within the boundaries
{
printf("Select an integer from 1 to 23: ");
scanf("%s", input);
}  
while (count != rows)  
{  
/* Do some stuff */  
}  
return 0;  
}
'
) //Check if any character has been inserted { printf("Enter an integer between 1 and 23: "); scanf("%s", input); } while(0 < rows && rows < 24) //check if the user input is within the boundaries { printf("Select an integer from 1 to 23: "); scanf("%s", input); } while (count != rows) { /* Do some stuff */ } return 0; }

Je l'ai fait à mi-parcours et un petit push up sera appréciée.

source d'informationauteur 7kemZmani