Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to validity input in c programming

Posted on 2004-11-17
9
Medium Priority
?
419 Views
Last Modified: 2010-05-18
#include <stdio.h>

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


 
0
Comment
Question by:tim9232
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 12610849
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
 

Author Comment

by:tim9232
ID: 12611113
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
 
LVL 11

Expert Comment

by:avizit
ID: 12611242
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 12611823
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
 

Author Comment

by:tim9232
ID: 12614226
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12622035
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
 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
ID: 12622065
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12625038
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question