Last active
September 26, 2024 16:16
-
-
Save paulido/edbcfe51cbf40cb156a7f7c7354c7755 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#hande input in c program | |
#allow on integer input | |
#@Author : Paul IDO | |
#@Email : [email protected] | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
// Permet de de saisie des nombre de longeur len | |
int input(int len){ | |
char c; | |
int ascii, count = 0, count2 = 0, valeur; | |
const int SIZE = len + 1; | |
char saisie[SIZE]; | |
saisie[0] = 'a'; | |
while (1) // saise | |
{ | |
c = getchar(); | |
// printf("le code ascii est : %d\n", ascii = (int)c); | |
if(c == '\n') | |
{ | |
if(strcmp(&saisie[0], "a") == 0) | |
return -1; //saisie incorrecte touche Entré appuyée : du vide | |
else if (count2 > count) | |
return -2; | |
else | |
break; | |
} | |
ascii = (int)c; | |
if(count > 0 && ascii == 8) // suppression | |
{ | |
saisie[count] = 'a'; | |
count --; | |
if(count = 0) | |
saisie[0] = 'a'; | |
} | |
if(ascii >= 48 && ascii <= 57) // un nombre saisi | |
{ if(count < len){ // la lougeur maximal de caractères autorisé n'est pas encore atteinte | |
saisie[count] = c; | |
count ++ ; | |
} | |
count2 ++; | |
} | |
else{ // caractères non autorisé saisie | |
if(count == len){ // nombre de caractères autorisés est atteint et la saisie continue: invalidé la saisie | |
count2 ++; | |
} | |
} | |
} | |
saisie[SIZE]='\n'; | |
valeur = atoi(saisie); | |
return valeur; | |
} | |
// Test | |
// int main(){ | |
// printf("%d", input(3)); | |
// return 0; | |
// } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment