-6

How can I scan 10 values from user in an array so that if user enter any value other than integer (i.e. float, char, special character, etc.(any other than integer)), then it display message and allow the user to enter corrected value ?

Pratik
  • 1
  • Possible duplicate of http://stackoverflow.com/questions/4072190/check-if-input-is-integer-type-in-c – Nishant Aug 07 '15 at 05:55

1 Answers1

3

use something like:

int a[10];               //array to input
int c=0;                 //counter that counts till 10 numbers
while(c<10){
    char s[10];          //assuming 10 is the max number of digits in your input number
    scanf("%10s",s);
    if(checkInt(s, strlen(s))){     //a function that you must write to check if input verifies your criteria of correct, like no alphabets, decimal, spl char etc. 
        a[c]=atoi(s);
        c++;
    }
    else{
        printf("Enter number in correct integer format\n");
    }
}

A possible checkInt function can be like this:

int checkInt(char s[], int l){
    int i;
    for(i=0;i<l;i++){
        if(s[i]<'0' || s[i]>'9')
            return 0;
    }
    return 1;
}
vish4071
  • 4,090
  • 2
  • 22
  • 55