Thread: How to use assert?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    How to use assert?

    I have this method:
    Code:
    void setvisible(Cell * C, int val)
    and i am asked to use assert to check that val is between 1 and 9 inclusive.

    I wrote
    Code:
    assert( (val >= '1') && (val <= '9') );
    Can someone please let me know if this is right, and if so what it actually does.

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    that looks right -- but beweare because assert does nothing at all when the program is compiled for release mode.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And you need <assert.h>.

    assert does nothing at all when the program is compiled for release mode.
    Or, rather, when NDEBUG is defined.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assert command
    By Albinoswordfish in forum C Programming
    Replies: 14
    Last Post: 12-24-2008, 09:03 PM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Customized assert question...
    By Raigne in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2008, 04:28 AM
  4. assert
    By George2 in forum C Programming
    Replies: 1
    Last Post: 10-22-2007, 03:17 AM
  5. Assert
    By Shamino in forum C++ Programming
    Replies: 8
    Last Post: 01-24-2006, 11:02 AM