Thread: If user enters wrong information in scanf

  1. #16
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Thanks guys, the error makes more sense now.

    All this pointer and array/string stuff is still over my head as I'm not up to there yet, but I will be soon. I will definitely come back and check this out when I know them in and out.

    Listen here you mucas pea-brained little insignificant ........ant of a human being. If you think you can manage this simple task, such that even a retarded chipmunk could perform, press the Y or N key now. If not, then do yourself a favor, unplug your computer and go jump off a bridge into freeway traffic.
    Press Y or N now:
    This is going into my program.

    If a bastard user can't respect my program and input a Y or an N, he's going to hear it.

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If a bastard user can't respect my program and input a Y or an N, he's going to hear it.
    You don't have to type that much you know, just a little nice error message:
    Code:
    fprintf(stderr, "Um...I said Y or N. You didn't do either. Now I'll have to cast you into the bowels of hell. Have a nice day. :-)\n");
    strlen(NULL);
    My best code is written with the delete key.

  3. #18
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    what is the strlen(NULL) for?

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is the strlen(NULL) for?
    Smiting sinful users.
    My best code is written with the delete key.

  5. #20
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I don't understand what you mean. Just stating bad practice. That line is useless

  6. #21
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually its far from useless. It's pretty much guaranteed to crash the program.

  7. #22
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by Prelude
    >what is the strlen(NULL) for?
    Smiting sinful users.
    Specifically smitten to the bowels of hell.

  8. #23
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Also known as the land of Barney the Dinosour

  9. #24
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Emmanuel Delaha
    Sounds terrible to me. The "%s" will try to convert the input to a string. Imagine you just hit 'y'<ENTER>. You will get a 'y' at 'response', and a 0 at response + 1 (I assume you know what a C-string is). The problem is that response + 1 is not defined. Writing to an undefined memory location invokes an undefined behaviour. It's a serious bug.

    At minimum, you want ;

    Code:
    	char response [2];
    
    	scanf(" %s", response);
    but using "%s" without a limit is dangerous (the gets() problem)
    Code:
    	scanf(" %1s", response);
    but it's hard to maintain (but there are some ugly tricks). For all these reasons (among others), the good way to get a line from a user (or more generally from a stream) is to use fgets() or a smart function of yours based on it.

    Well thank you for the correction. You explained it quite well actually.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  2. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  3. Replies: 6
    Last Post: 07-10-2002, 07:45 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. C++ Help
    By ravie23 in forum C++ Programming
    Replies: 0
    Last Post: 12-18-2001, 02:50 AM