Thread: scanf problem!!!

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    scanf problem!!!

    printf(" please insert integer number " );

    scanf("%d",&k))

    if user insert character or alphabet, the program will hang there.
    So, do anybody can suggest how to over this problem.
    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    Code:
    #include <stdio.h> //Necessary for all programs to work
    
    int main(void)    { //Necessary for all programs to work
    
        int k; //You must initialize your variables
    
        printf("Please insert an integar.\n"); //Follow proper printf format
        scanf("%d", %k); //same for scan f
    
        return 0; //must include
    } //must include

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Read a string instead, and then use atoi on it to get the integer.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Read a string instead, and then use atoi on it to get the integer.
    So tell me. How is the nonexistent error handling and potential undefined behavior of atoi any improvement?

    >So, do anybody can suggest how to over this problem.
    It depends on what you're doing. In most cases your best bet is to read a line using fgets, validate your input as necessary, and make the appropriate conversions. Alternatively you could simply discard any input in the stream buffer and try again if scanf fails. Can you be more specific about your needs?
    My best code is written with the delete key.

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Yes it's a hack, but at least it won't give crazy values.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Yes it's a hack, but at least it won't give crazy values.
    You must have a different concept of undefined behavior than I do. Though I will agree that if the user enters non-numeric characters, atoi is very predictable in returning 0.

    By the way, a hack is clever. This is just stupid because you're effectively putting on blinders and pretending that broken code is working.
    My best code is written with the delete key.

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    A hack can also be something put together in a crude manner, which is what I was thinking of. I didn't know that atoi() had an undefined error handling, so how about strtol() or sscanf()?

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by OnionKnight
    A hack can also be something put together in a crude manner, which is what I was thinking of.
    Not really. When something is considered a hack, yes it's done in a crude manner, but it still gets the job done correctly. It may not be fast or efficient and it may be horribly sloppy to work with, but the eventual product works.
    Sent from my iPadŽ

  10. #10
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    How does that differ from what I said? When you "hack something together" you're doing it with speed so naturally things like error checking gets left behind.

  11. #11
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Let's not turn this thread into a debate about the definition of hack and keep it on topic?

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by s3ng
    printf(" please insert integer number " );

    scanf("%d",&k))

    if user insert character or alphabet, the program will hang there.
    So, do anybody can suggest how to over this problem.
    Thanks
    if you have int k, change it to char k. then %c instead of %d.

    I dont know what everybody else got into, but they made it way too complicated.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    15
    Quote Originally Posted by s3ng
    printf(" please insert integer number " );

    scanf("%d",&k)) <====Take note of this line

    if user insert character or alphabet, the program will hang there.
    So, do anybody can suggest how to over this problem.
    Thanks
    I assumed tat u have include the neccessary header files *.h.

    This is the correct format ------ scanf("%d",&k); <====Take note of the brackets and the semi-colon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 01:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-05-2005, 12:42 AM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM