Anyone knows how to allow user to only input numbers(floats) to a system?

This is a discussion on Anyone knows how to allow user to only input numbers(floats) to a system? within the C Programming forums, part of the General Programming Boards category; Hello... Anyone here familiar with how we can limit the user from typing in any other characters into a system ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    Question Anyone knows how to allow user to only input numbers(floats) to a system?

    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24
    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.

    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;
    }
    The output of the code is :

    Code:
    enter anything    2525
    entered value is 2525.000000
    
    enter anything a2525
    Entered value contains alphabets or symbols
    Waiting for reply from other members too.

    REALNAPSTER
    Last edited by realnapster; 05-01-2006 at 05:07 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  2. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  3. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21