C++ help

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

  1. chris200x9 macrumors 6502a

    Jun 3, 2006
    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
  2. kainjow Moderator emeritus


    Jun 15, 2000
    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:
    int number = 5;
    and a double equal (==) is equality:
    if (number == 5) ...
  3. tutubibi macrumors 6502a


    Sep 18, 2003
    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


    Jul 18, 2002
    because you're not reading the compiler warnings.
  5. charray macrumors newbie

    Sep 10, 2007
    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

    Jun 3, 2006

Share This Page