Advertisement
ppupil2

check-input_TuanVM

May 1st, 2020
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. //luu y chi dung 1 ham check
  7. //Ham check 1
  8. int check(){
  9.     int check=0;
  10.     char s[11];//do kieu int bang hon 2 ty nen toi da la 10 ki tu so nhap vao
  11.     int i=0;
  12.     char c;
  13.     do{
  14.        
  15.         while((c=getchar())!='\n'){//nhap tung ki tu tu ban phim
  16.             if(isdigit(c)){
  17.                 s[i++]=c;//do tung ki tu vao xau
  18.                 check=1;//danh dau la dang nhap so
  19.             }else{
  20.                 i=0;//gan lai
  21.                 check=0;//ki tu nhap vao sai , nhap lai
  22.                 break;
  23.             }
  24.         }
  25.         if(check==0){
  26.             printf("Enter again.\n");
  27.             fflush(stdin);
  28.         }
  29.        
  30.     }while(check==0);
  31.     int n=atoi(s);//chuyen xau sang so
  32.     return n;
  33.    
  34. }
  35.  
  36. //ham check 2
  37. int inputCheck(int min, int max,char msg[],char err []){
  38.     int num;
  39.     int check;
  40.     char c;
  41.     do{
  42.         printf("%s",msg);
  43.         check=scanf("%d%c", &num, &c);//Returns the value of an integer
  44.         fflush(stdin);
  45.         if(check!=2||c != '\n' || num<min || num>max){
  46.             printf(err);
  47.             fflush(stdin);//Delete buffer
  48.             check=0; //input is a character
  49.         }
  50.         else{
  51.             check=1; //input is a number
  52.         }
  53.     }while(check==0);
  54.     return num;
  55. }
  56.  
  57. int main(){
  58.    
  59.     int n=check();
  60.     printf("%d",n);
  61.  
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement