Thread: Struct File and array problem Please help

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by caroundw5h View Post
    you could make it even more compact
    Code:
    #define FLUSH while ( (getchar() ! = '\n') )
    and then call FLUSH whenever you want to clean out the buffer.
    And what will happen to this more compact code when stdin will be redirected to read from file and actually hits the EOF?

    Getting code shorter by adding more bugs is not a good way to program
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by vart View Post
    And what will happen to this more compact code when stdin will be redirected to read from file and actually hits the EOF?
    where in his code is he redirecting stdin to read from a file (unless you mean waiting for the EOF to to be hit via the terminal)? His objective was to get the junk out of the input buffer and clean it up. To that end he can read up to the '\n' after each input. Sure he could add the EOF to that as well to that macro, just seems like overkill IMHO.

    Getting code shorter by adding more bugs is not a good way to program
    I agree whole heartedly, the objective on my part wasn't to offer a quick fix and forget potential future issues, it was to generate compact code for the task at hand. Why declare a long, when a int with do?
    Warning: Opinions subject to change without notice

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

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by caroundw5h
    where in his code is he redirecting stdin to read from a file (unless you mean waiting for the EOF to to be hit via the terminal)?
    The redirection can happen from the terminal/console/command prompt, not just the code.

    Quote Originally Posted by caroundw5h
    His objective was to get the junk out of the input buffer and clean it up. To that end he can read up to the '\n' after each input. Sure he could add the EOF to that as well to that macro, just seems like overkill IMHO.
    vart's point is about correctness. It might be a little paranoid, but the "overkill" will have no impact on performance anyway, and remains readable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  2. File I/O problem for dynamically allocated struct array
    By veecee in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 09:28 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM