Thread: Accepts only integer

  1. #1
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16

    Accepts only integer

    Hi to all!
    Do you know how to accept only integer and if the user enters a character, the inputted character will not appear, instead the program will display an error message.
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    accept the keyboard input as a string then validate each character of the string. There is no standard C function to get just one charcter from the keybord at a time. But if you are not concerned about ascii C, then use functions in conio.h if your compiler supports them.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    I dont know if you can use:

    _getch() and _putch();

    But look into them.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( isfriend( isdigit ) )
    {
        printf("isdigit is your friend!\n");
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could also check the return value of scanf() if you want to input a number.

    BTW, isdigit() is in <ctype.h>.

    There is no standard C function to get just one charcter from the keybord at a time.
    _getch() and _putch();
    Nothing was said about one character. I think Alexthunder wants isdigit() or scanf().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by quzah
    Code:
    if( isfriend( isdigit ) )
    {
        printf("isdigit is your friend!\n");
    }
    By the way, isfriend() is not an ANSI function. One possible implementation of it could be:
    Code:
    int isfriend(int(*func)(int))
    {
        return (func == isdigit);
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Thanks, I needed a bit of cheering up, after how angry someone here was making me by you know, handing out answers to homework again...


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16
    Thanks for the replies. Anyway in the
    Code:
    int isfriend(int(*func)(int))
    {
        return (func == isdigit);
    }
    is func a variable that you can use in scanf?
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, that was a joke. It's a function pointer.


    Go look up the 'isdigit' function, and you'll see that you pass it a character you want to test, and it tells you if it's a digit or not.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Ancient Dragon
    aThere is no standard C function to get just one charcter from the keybord at a time.
    huh?

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by kermit
    huh?
    Buffered input, etc. Keyboard != stdin.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by kermit
    huh?
    Re-read the original question. If the user presses a non-digit key he wants to display an error message. None of the functions in stdio.h will allow that functionality. Sure, scanf() will stop reading from the keyboard when the first non-digit key is pressed but that is not the behavior needed by the op's question. getchar() will return just one character -- but you have to press <Enter> which makes two keystrokes, not one. In 32-bit programs conio.h contains non-standard getch() that will do what the op needs, but as we all know not every compiler will support that function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM