开发者

C++ If and if else error messages

I am making a collection of math functions. I used if statements to create error messages开发者_开发问答 and cut down on user errors.. I don't think I did it right.

string error_0;
string error_i;

if (num1 == 0;) {error_0 = "You cannot divide by zero. Fail."}
    else {error_0 = "";}

if (num1=<0;) {error_i = "Sorry, can't do imaginary numbers."} //for sqroot function
    else {error_i = "";}

if (num2=<0;) {error_i = "Sorry, can't do imaginary numbers."}
    else {error_i = "";}

... this gives me the following error messages in code::blocks compiling.

  • expected ")" before ";" token (the if lines)
  • expected primary expression before ")" token (the if lines)
  • else without a previous if (just for the last else)

I'm really new at C++ and from the examples I have everything looks correct. Help would be appreciated. Thanks.


You need a semicolon after your strings and don't need one inside the if conditions. Also, the operator is <=, not =<.

string error_0;
string error_i;

if (num1 == 0) {error_0 = "You cannot divide by zero. Fail.";}
    else {error_0 = "";}

if (num1 <= 0) {error_i = "Sorry, can't do imaginary numbers.";} //for sqroot function
    else {error_i = "";}

if (num2 <= 0) {error_i = "Sorry, can't do imaginary numbers.";}
    else {error_i = "";}


Don't put semi-colons in '()' brackets. On the other hand, you have to end each instruction with a semi-colon (and you didn't do it in the first '{}' brackets). It should be:

if (num1 == 0) {error_0 = "You cannot divide by zero. Fail.";}
else {error_0 = "";}

BTW, you don't have to use brackets if you use a simple instruction after if or else, it can be as well:

if (num1 == 0)
    error_0 = "You cannot divide by zero. Fail.";
else
    error_0 = "";


string error_i;
if (num1=<0;) {error_i = "Sorry, can't do imaginary numbers."}
else {error_i = "";} 
  • the comparison operator you want is <= (think less than or equal) rather than =<
  • parenthesised expressions don't need semicolons but statements do
    • here if (expression) { [first-statement-when-true; [second...;]] } else { [first-statement-when-false; [second...;]] }

That's enough to get it to work, but std::strings default-constructor themselves to be "" anyway, so you don't need the else { error_i = ""; } code at all...

string error_i;

if (n <= 0)
    error_i = "Sorry, can't do imaginary numbers.";

OR, you can capture the this-or-that feel of your original and immediately give error_i a meaningful value as in:

string error_i = n <= 0 ? "Sorry, can't..." : "";

Above, ? and : select the value between or after them based on the truth of the prior expression (n <= 0), then that is assigned as the initial value of error_i. Some people would think this is bad because setting a std::string to "" is pointless and possibly slightly inefficient (they're empty after default construction anyway), but having code that clearly expresses your thinking about the program is usually more important.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜