Thread: Help With Doorknob-Proofing A Program

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Calgary, Alberta
    Posts
    9

    Help With Doorknob-Proofing A Program

    Hey folks,

    I've finally gotten my program working. Yay! Now the only thing I'm having problems with is when people do what they aren't supposed to.

    Specifically, if a user types in any letters at the line breaks, or when they're supposed to submit a number, it gives me a loop of doom. From what I can tell it's sending back the error messages for if the number submitted isn't valid, except it doesn't stop. It's probably having a problem with figuring out whether the ANSI code for the letters is valid, or something along those lines. At least I think that makes sense.

    Anyway, any feedback to catch this problem, and of course anything to clean up the coding wherever possible is greatly appreciated.

    The code's quite long, so it's attached as a .c file.

    Thanks again for the help.

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    wow nice fun game! lol! i like it!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. Fix the indentation.

    > Specifically, if a user types in any letters at the line breaks, or when they're supposed to submit a number, it gives me a loop of doom
    Mixing fgets(), getchar() and scanf() will always lead you into trouble.

    Code:
    if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
      if ( sscanf( buff, "%d", &myint ) == 1 ) {
        // myint is valid, and there's no messy input remaining on stdin
      }
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM