Thread: C++ If Statements Help

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by moporho View Post
    Todd,

    I am sorry to have bothered you. I am just not getting what you are saying. Should I be putting "=" signs in too? Maybe "<="?
    Perhaps, but initializing "middle" to (any) one of the input values should work fine too

    I am trying to get the info you are providing, but I am letting the rude comments of Elysia interfer.

    Thanks again,
    Mary Pat
    I know it may sound like Elysia is rude [he/she may even BE rude], but correctly indenting the code is part of writing good code, so you should really try to understand:
    How to make the editor indent the code correctly and/or why the code doesn't look correct when you post it here.

    The reason indentation is important is that it's showing how the if-statements relate to the actual code.

    Also, I don't actually think you need if-statements "inside each other" (nested if's) to solve this problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by moporho View Post
    I am trying to get the info you are providing, but I am letting the rude comments of Elysia interfer.
    Then maybe I should retort that you are rude to ignore everything I've written thus far, not even a single acknowledgment even that you saw what I wrote but want to focus on something else first.
    Indentation is a very important thing because it makes your code readable.
    Last edited by Elysia; 01-18-2008 at 01:10 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Elysia has a lot to offer us all. If Elysia's comments are annoying you, take with 1 grain of salt.

    You do have a nested IF going on there.

    I'm going to simplify the logic. You don't need to have all the variables sorted, just just need to find the middle value.

    This logic will work. See if you can implement this logic:

    if number1 is less than than or equal to number2 AND number2 is less than or equal to number3 then number2 is the middle number.

    else if number2 is less than than or equal to number1 AND number1 is less than or equal to number3 then number1 is the middle number.

    else number3 is the middle number.

    Todd

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Todd Burch View Post
    This logic will work. See if you can implement this logic:

    if number1 is less than than or equal to number2 AND number2 is less than or equal to number3 then number2 is the middle number.

    else if number2 is less than than or equal to number1 AND number1 is less than or equal to number3 then number1 is the middle number.

    else number3 is the middle number.

    Todd
    This logic won't work. (Consider number1 = 9, number2 = 7, and number3 = 5.) But the idea is right: to be the middle number, you have to have one number greater and one number less. You need to add the symmetric cases as well (that is for example, number2 is the middle number if either number1 <= number2 <= number3, as given above, or number3 <= number2 <= number1).

  5. #20
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by tabstop View Post
    This logic won't work.
    Oops. You're rightll sorry MP.

    (that is for example, number2 is the middle number if either number1 <= number2 <= number3, as given above, or number3 <= number2 <= number1).
    MP, don't interpret this that number1 <= number2 <= number3 is valid C syntax for what you want to do - you had that before, and while we can all visualize and understand what it means, it's not code you want to implement verbatim.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  4. Statements and Functions
    By GeekFreak in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2002, 12:34 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM