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 ?
Asked
Active
Viewed 88 times
1 Answers
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;
}
![](../../users/profiles/2453089.webp)
vish4071
- 4,090
- 2
- 22
- 55
-
to anyone who downvoted this, can you tell why this does not work in given scenario?? – vish4071 Aug 07 '15 at 06:00
-
-
checkInt is something that you will have to write...I'll edit the answer and write a possible checkInt for you. – vish4071 Aug 07 '15 at 06:08
-
-
-