Execution fails to enter if statement when it should

This is a discussion on Execution fails to enter if statement when it should within the C++ Programming forums, part of the General Programming Boards category; I have the following code: Code: cout<< "num_of_spcs_to_add is: " << num_of_spcs_to_add <<endl; cin.get(); if ((num_of_spcs_to_add != 0) && (!num_of_spcs_to_add ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Unhappy Execution fails to enter if statement when it should

    I have the following code:

    Code:
    cout<< "num_of_spcs_to_add is: " << num_of_spcs_to_add <<endl;
                             cin.get();
                             if ((num_of_spcs_to_add != 0) && (!num_of_spcs_to_add < 0)) { //this prevents trying to fix a column row which doesn't need any fixin'
                                cout<< "if statement entered." <<endl;
                                //fix current row of column:
                                for (size_t i = 0; i < num_of_spcs_to_add; i++) {
                                    spcs_str += ' ';
                                }
                                current_subtype_str.insert(0, spcs_str); //add spaces in front of column row string so we can shift column row to the right
                                //fix current line:
                                buffer_str.replace(subtype_start_pos, current_subtype_str_size, current_subtype_str);
                                cout<< "buffer_str is: " << buffer_str <<endl;
                                cin.get();
                                current_subtype_str_size = current_subtype_str.size(); //since it just changed
                                complete_file_str->replace(original_line_pos, buffer_str_size, buffer_str); //replace the current line in the complete file's string
                                                                                                            //with the new version, which has the necessary
                                                                                                            //spaces added
                                buffer_str_size = buffer_str.size(); //since it just changed
                             }
    and output shows that "num_of_spcs_to_add" is 3 at a particular time. Therefore the following if statement "if ((num_of_spcs_to_add != 0) && (!num_of_spcs_to_add < 0))" SHOULD be entered, since num_of_spcs_to_add is both not 0 and not less than 0. However, it doesn't, which is shown since neither output statement is executed.

    Now can anyone please explain why the if statement is not being entered?

    Thanks in advance.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    Do you really mean !num_of_spcs_to_add ?

    Jim

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > (!num_of_spcs_to_add < 0)
    You probably want different precedence on this part of the expression.

    If you were after !(x<0), why didn't you just do it all in the first part anyway?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    O_o

    Because zero is rarely if ever less than zero.

    Soma

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    Do you really mean !num_of_spcs_to_add ?

    Jim
    I tried that too, along with (!num_of_spcs_to_add == 0), however the if statement still was not entered.
    Quote Originally Posted by Salem View Post
    > (!num_of_spcs_to_add < 0)
    You probably want different precedence on this part of the expression.

    If you were after !(x<0), why didn't you just do it all in the first part anyway?
    Thanks. That solved it.
    I switched (!num_of_spcs_to_add < 0) to !(num_of_spcs_to_add < 0), and now the if statement is being entered.
    Quote Originally Posted by phantomotap View Post
    O_o

    Because zero is rarely if ever less than zero.

    Soma
    Well, num_of_spcs_to_add is not 0 nor is it less than 0, its 3.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > if ((num_of_spcs_to_add != 0) && !(num_of_spcs_to_add < 0))
    Isn't this just
    if ( num_of_spcs_to_add > 0)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Salem View Post
    > if ((num_of_spcs_to_add != 0) && !(num_of_spcs_to_add < 0))
    Isn't this just
    if ( num_of_spcs_to_add > 0)
    Oops...you're right. It is. Duh.
    My bad.

    Thanks for pointing out my error. Its always best to write expressions the easiest way possible.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiles good but fails in execution
    By Arjit in forum C Programming
    Replies: 2
    Last Post: 05-01-2010, 02:23 AM
  2. Mesure time elapsed for each statement execution
    By reg13 in forum C Programming
    Replies: 2
    Last Post: 08-20-2009, 11:04 PM
  3. Replies: 9
    Last Post: 02-22-2009, 10:50 PM
  4. trying to rea from /dev/tty fails!
    By arunj in forum Linux Programming
    Replies: 4
    Last Post: 10-17-2007, 03:59 AM
  5. help with if statement and ENTER
    By xhoangx in forum C Programming
    Replies: 4
    Last Post: 12-05-2005, 02:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21