C++ help

Discussion in 'Mac Programming' started by chris200x9, Sep 9, 2007.

  1. chris200x9 macrumors 6502a

    Joined:
    Jun 3, 2006
    #1
    Hi I'm taking a class at school and this isn't really an assignment but sorta is and sorta me just wanting to learn so you don't have to help but I would appreciate it. I am trying to create an if else statement so if you don't know the height and base you can just input the three sides. I need help I keep getting errors so far and I don't know why? I realize I'm not finished writing the code but I really want to get help now so I actually understand what I'm doing wrong.
     

    Attached Files:

    • cpp.txt
      File size:
      462 bytes
      Views:
      58
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Your syntax is incorrect. See Branching Statements (if, else, switch) for the correct syntax.

    Also, the equal (=) sign has two different uses. A single equal (=) is assignment:
    Code:
    int number = 5;
    and a double equal (==) is equality:
    Code:
    if (number == 5) ...
     
  3. tutubibi macrumors 6502a

    tutubibi

    Joined:
    Sep 18, 2003
    Location:
    localhost
    #3
    Yes, definitely look at the syntax first.

    BTW, you can not compare strings with == in C++, you will have to use some function, for example strstr.
    Or, limit your input to a single char ('Y' or 'N') and then you can compare char, i.e.

    if( inputchar == 'Y' ) {
    // logic for YES ...
    } else {
    // logic for NO ...
    };
     
  4. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #4
    because you're not reading the compiler warnings.
     
  5. charray macrumors newbie

    Joined:
    Sep 10, 2007
    #5
    Wrong. == can be used to compare strings, as long as it is a std::string class. Don't mix up Java with C++. :p

    Some points:
    1. height, not hieght
    2. It is not reasonable to ask if the user knows the height and base - there are not any alternatives to get the answer.
    3. bool type has only true and false. It would be wise to take input as string instead of bool.
    4. It is syntax correct to say "if (answer = yes)". Sometimes it is useful but not this time. Use == for comparison.
    5. Use double quote (") to quote strings, single (') for character. Make sure you know they are different.
     
  6. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006

Share This Page