macro help

Discussion in 'C++' started by ankitks@yahoo.com, Oct 22, 2006.

  1. Guest

    guys,
    I have to do some testing, and usally code flows like this. If
    something is wrong, flag error. And if it is correct, see if something
    else is wrong, flag error! And recursive like that.

    if (a != b) {
    flag_error();
    }
    else {
    if ( c != d) {
    flag_error();
    }
    else {
    if ( e != f) {
    flag_error();
    }
    } //end of else c != d
    } //end of else a !=b

    it is really annoying to read code later for debugging. Using a !=b ?
    flag_error() : c != d ? ...doesn't seem that practical either.
    how can I convert this into macro so that I can use it more readable
    something like this!

    VERIFY a == b OTHERWISE flag_error();
    GO_NEXT;
    VERIFY c == d OTHERWISE flag_error();
    GO_NEXT;
    VERIFY e == f OTHERWISE flag_error();
    is this recursive macro possible.

    Sorry, for asking dumb quesitons. Trying to make life easy, if possible.
    , Oct 22, 2006
    #1
    1. Advertising

  2. wrote:
    > guys,
    > I have to do some testing, and usally code flows like this. If
    > something is wrong, flag error. And if it is correct, see if something
    > else is wrong, flag error! And recursive like that.
    >
    > if (a != b) {
    > flag_error();
    > }
    > else {
    > if ( c != d) {
    > flag_error();
    > }
    > else {
    > if ( e != f) {
    > flag_error();
    > }
    > } //end of else c != d
    > } //end of else a !=b
    >


    What's wrong with (it seems at least as readable as your proposed solution):
    if (a != b) {
    flag_error();
    }
    else if ( c != d) {
    flag_error();
    }
    else if ( e != f) {
    flag_error();
    }
    else
    {
    //If we get here, nothing is wrong
    }

    Or, using exceptions:
    if (a != b) {
    throw some_error;
    }
    if ( c != d) {
    throw some_error;
    }
    if ( e != f) {
    throw some_error;
    }
    //If we get here, nothing is wrong

    --
    Clark S. Cox III
    Clark S. Cox III, Oct 22, 2006
    #2
    1. Advertising

  3. David Harmon Guest

    On 21 Oct 2006 18:45:13 -0700 in comp.lang.c++,
    wrote,
    >I have to do some testing, and usally code flows like this. If
    >something is wrong, flag error. And if it is correct, see if something
    >else is wrong, flag error! And recursive like that.


    It's actually not recursive; rather, it's alternative.

    My version:

    if ((a != b)
    || (c != d)
    || (e != f)) flag_error();
    David Harmon, Oct 22, 2006
    #3
  4. Fox Guest


    > if ((a != b)
    > || (c != d)
    > || (e != f)) flag_error();


    That's good if there is one error to all cases.
    Fox, Oct 22, 2006
    #4
  5. David Harmon Guest

    On Sun, 22 Oct 2006 12:51:11 +0200 in comp.lang.c++, "Fox"
    <> wrote,
    >
    >> if ((a != b)
    >> || (c != d)
    >> || (e != f)) flag_error();

    >
    >That's good if there is one error to all cases.


    That seemed to be the example given.
    If not, then my next suggestion is:

    if (a != b) return flag_error();
    if (c != d) return flag_error();
    if (e != f) return flag_error();
    David Harmon, Oct 22, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,107
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    576
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    500
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    469
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    457
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page