Link to home
Start Free TrialLog in
Avatar of tim9232
tim9232

asked on

How to validity input in c programming

#include <stdio.h>

int a;
   printf("Please enter a number);
   scanf("%d", &a);//validity input(both character and negative number)


 
Avatar of avizit
avizit

There are lot of functions to do that .. you need not write your own code,.


 #include <ctype.h>

       int isalnum(int c);
       int isalpha(int c);
       int isascii(int c);
       int isblank(int c);
       int iscntrl(int c);
       int isdigit(int c);
       int isgraph(int c);
       int islower(int c);
       int isprint(int c);
       int ispunct(int c);
       int isspace(int c);
       int isupper(int c);
       int isxdigit(int c);
Avatar of tim9232

ASKER

When I using isalpha it generate an error so how do i modify the code to produce the result

#include <stdio.h>

int a;
   printf("Please enter a number: ");
   scanf("%d", &a);//validity input(both character and negative number)
   if(isalpha(a)>=0)
      printf("Error");
   else
      printf("%d", a);
   fflush(stdin);
   getchar();
}

the result should be like that:

Please enter a number : k (when i input k)
Error(the result should be like that)

Please enter a number: 98 (when i input 98)
98(the result that when i input)
you have

scanf("%d", &a);  

so scanf expects only integers , so if you enter a alphabetic character your scanf fails.

so what I would sugest is you get the input as a string , and if you find it doesn't contain alphabetic characters you can then
use the atoi() function to get the integer ( there are other risks here like if the character is neither alphabetic or numeric , but that you can fix yourself once you get the hang )

so you can use gets()  
try the following program and modify according to your needs


++++++++++++++++++++++++++++++
#include <stdio.h>
int main(){
  char a[20];
  int b;
  printf("Please enter a number: ");
  gets(a);

  int i = 0;
  while(a[i]){
    if(isalpha(*a)){
      printf("error");
      exit(1);
    }
    i++;
  }

  b = atoi(a);
  printf("%d\n", b);
  getchar();
  return 0;
}
++++++++++++++++++++++++++++++++++++++++
notes:
1) the function gets() has some issues ..it doesn't check for buffer overflows .
2)   char a[20]; <-- so you can enter only  a fixed number of characters , set it to sufficently high enough for your needs
3) fflush(stdin) <-- although its works in MS VC++ , its not standard , the ANSI C standard says  fflush is undefined for stdin
read this
http://www.eskimo.com/~scs/C-faq/q12.26.html
Avatar of sunnycoder
Hi tim9232,

If strtol function is available on your platform - its a POSIX standard, so it should be available on any POSIX compliant platform - then you can do the validation with minimum amount of code writing on your part ...

1. Use fgets to read in the number as a string into a buffer
2. Use strtol to convert it to a long. Check the return value
3. Check the value pointed by endptr .... If it is not '\0' or 0 or ' ', then probably you had incorrect input
4. Checking for -ve values is now trivial

Refer to the man page for strtol. Pay attention to the part that says
If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not `\0' but **endptr is `\0' on return, the entire string is valid.

http://www.die.net/doc/linux/man/man3/strtol.3.html

cheers
sunnycoder
Avatar of tim9232

ASKER

Thx
If the input data is invalid, error message will appear and asks the user to input again the data again.

printf("Enter a number: ");
  scanf("%d", &i);

  printf("Enter other number: ");
  scanf("%d", &j);

  printf("Enter a character: ");
  fflush(stdin);
  scanf("%c", &a);

Once error is detected, adding a printf is trivial !! ... I am not sure what you are trying to ask since do not see any error detecting code in snippet you posted.
ASKER CERTIFIED SOLUTION
Avatar of avizit
avizit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
scanf returns you the number of fields succesfully converted. So you can use little loops like these:

do {
  printf("Enter a number: ");
} while (scanf("%d", &i)!=1);

do {
  printf("Enter other number: ");
} while(scanf("%d", &j)!=1);

do {
  printf("Enter a character: ");
  fflush(stdin);
} while(  scanf("%c", &a)!=1);