Hello...
Anyone here familiar with how we can limit the user from typing in any other characters into a system other then numbers. If he/she does, the program will print an error message.
Thank You![]()
Hello...
Anyone here familiar with how we can limit the user from typing in any other characters into a system other then numbers. If he/she does, the program will print an error message.
Thank You![]()
You're not going to believe this!
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
This code provides a solution to your problem. It takes the input into a charecter array(intentionally) and checks the ASCII value of each entered value. If the value is not B/W 48 to 57 then it shows the error message. 48 to 57 are the ASCII values for numbers 0 to 9.
The output of the code is :Code:#include <stdlib.h> #include <stdio.h> #include<string.h> int main(void) { float f; int i=0; char str[10]; printf("\n enter anything \t"); fgets(str,sizeof(str),stdin); while(i<strlen(str)) { if(str[i]<48 || str[i]>57) { printf("\n Entered value contains alphabets or symbols"); getchar(); exit(0); } i++; } f = atof(str); printf("entered value is = %f\n",f); getchar(); return 0; }
Waiting for reply from other members too.Code:enter anything 2525 entered value is 2525.000000 enter anything a2525 Entered value contains alphabets or symbols
REALNAPSTER
Last edited by realnapster; 05-01-2006 at 05:07 AM.
Perhaps you should read this as well
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.