1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Nettoyage après utilisation de fgets() */
void fclean(char *s_tampon, FILE *fp)
{
/* On recherche le caractère de fin de ligne dans le tampon */
char *pc = strchr(s_tampon, '\n');
if (pc != NULL) /* Si le caractère de fin de ligne a été trouvé... */
{
/* ... on l'efface */
*pc = 0;
}
else /* Sinon */
{
/* On vide le tampon du flux entrant (fichier ou entrée standard)*/
int c;
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
continue;
}
}
}
int main(void)
{
char s_tampon[15] = {0};
char *pend = NULL;
int nombre = 0;
do
{
printf("Veuillez entrer un nombre: ");
fflush(stdout);
if (fgets(s_tampon, sizeof s_tampon, stdin) != NULL)
{
fclean(s_tampon, stdin);
nombre = strtol(s_tampon, &pend, 0);
}
} while (*s_tampon == 0 || *pend != 0);
printf("Le nombre que vous avez entré est %d\n", nombre);
return EXIT_SUCCESS;
} |
Partager