PDA

View Full Version : C++ help




chris200x9
Sep 9, 2007, 08:06 PM
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.



kainjow
Sep 9, 2007, 09:01 PM
Your syntax is incorrect. See Branching Statements (if, else, switch) (http://www.intap.net/~drw/cpp/cpp04_02.htm) 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) ...

tutubibi
Sep 9, 2007, 09:44 PM
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 ...
};

zimv20
Sep 9, 2007, 10:49 PM
I keep getting errors so far and I don't know why?
because you're not reading the compiler warnings.

charray
Sep 10, 2007, 05:36 AM
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.

chris200x9
Sep 10, 2007, 04:10 PM
thanx everyone :D