• C

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)


 
tim9232Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

avizitCommented:
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);
0
tim9232Author Commented:
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)
0
avizitCommented:
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
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

sunnycoderCommented:
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
0
tim9232Author Commented:
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);

0
sunnycoderCommented:
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.
0
avizitCommented:
even i am unsure what you want , but if you want to ask again if there is an invalid input you can put that in a while loop as belows

int valid = 0;

while(!valid){
   
   1. ask user to enter input
   2. read input
   3.  if ( input valid ){
       valid = 1;
    }

}

the above is just a outline
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jaime OlivaresSoftware ArchitectCommented:
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);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.