How to control the input ?

This is a discussion on How to control the input ? within the C Programming forums, part of the General Programming Boards category; Hi, Code: scanf("%d",&x); How can I control the input ? Code: if (x is a number) { //do this } ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    25

    How to control the input ?

    Hi,

    Code:
    scanf("%d",&x);
    How can I control the input ?
    Code:
    if (x is a number) {
    //do this 
    } else {
    //"I don't accept char!";
    }
    Thank you
    Last edited by bchaib; 08-11-2008 at 03:22 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    use fgets/strtol pair with the full error checking procedure
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    isanum()

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    Quote Originally Posted by vart View Post
    use fgets/strtol pair with the full error checking procedure
    Quote Originally Posted by valaris View Post
    isanum()
    Yes, but how to use it, pleas? and with wich library ?

    Thank you

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    but how to use it
    this forum has a search feature... use it, write your own code - post it if you have some problems
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by valaris View Post
    isanum()
    There's no such thing in the standard library, that's for sure. Read string [e.g. using fgets() ] then use strtol() to translate the string into a number. This has, as vart says, been discussed MANY times.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by matsp View Post
    There's no such thing in the standard library, that's for sure. Read string [e.g. using fgets() ] then use strtol() to translate the string into a number. This has, as vart says, been discussed MANY times.

    --
    Mats
    Sorry, I think i meant isdigit

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by valaris View Post
    Sorry, I think i meant isdigit
    But isdigit doesn't take a string, it takes a single char. You can of course use it once you have a string that MAY be a number - in whihc case you'd have to loop around checking each character. strtol() will do the looping for you, along with the process of converting it to an integer. Much easier, in my opinion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    Quote Originally Posted by vart View Post
    this forum has a search feature... use it, write your own code - post it if you have some problems
    Yes I know and yes I did search for how 2 use fgets, but I didn't gbet anything.

    My wone code?

    Code:
    int x = 0;
    
      scanf("%d",&x);
      printf("Root of %d is %d",x,sqrt(x));
    And what I want to code is:

    Code:
    int x = 0;
      scanf("%d",&x);
    
      if (x is a number) {
        printf("Root of %d is %d",x,sqrt(x));
      } else {
        //wrong
      }

    Greetings

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Why are you doing this check? x will always be a number :P

    If you want to get a charcater from the user youre probably best off using getchar()
    You can then test if its a number using isdigit() from ctype.h

    If you want to get a string then theres an example of how to use fgets in the FAQ

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    Quote Originally Posted by mike_g View Post
    Why are you doing this check? x will always be a number :P

    If you want to get a charcater from the user youre probably best off using getchar()
    You can then test if its a number using isdigit() from ctype.h

    If you want to get a string then theres an example of how to use fgets in the FAQ
    I think you don't understand my question at all

  12. #12
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    The value scanf/sscanf return is the number of items succesfully read. So, what you want is something like

    Code:
    int nbItemsRead, x;
    nbItemsRead = scanf("%d", &x);
    // Here you should clear the input buffer if you use scanf instead of fgets + sscanf...
    // But I'm not telling you how...
    if (nbItemsRead == 0)
    {
       // The user didn't enter a number
    }
    
    else
    {
       // The user entered a number
    }
    I hate real numbers.

  13. #13
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by matsp View Post
    But isdigit doesn't take a string, it takes a single char. You can of course use it once you have a string that MAY be a number - in whihc case you'd have to loop around checking each character. strtol() will do the looping for you, along with the process of converting it to an integer. Much easier, in my opinion.

    --
    Mats

    Ahh yes true, I just figured this would be acceptable since he is inputing an integer, not a string.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    Quote Originally Posted by foxman
    // Here you should clear the input buffer if you use scanf instead of fgets + sscanf...
    // But I'm not telling you how...
    You mean if I use fgets in stead of scanf ?

  15. #15
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    yes, just use fgets. you can find out how to use it in the FAQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  2. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  3. edit control - testing input
    By Micko in forum Windows Programming
    Replies: 2
    Last Post: 08-04-2004, 01:23 AM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 12:50 PM
  5. Problem with changing input focus
    By Clyde in forum Windows Programming
    Replies: 2
    Last Post: 05-25-2002, 05:06 AM

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