C++ If 和 if else 错误消息
我正在制作数学函数的集合。我使用 if 语句来创建错误消息并减少用户错误。我认为我做得不对。
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 = "";}
...这在 code::blocks 编译中给了我以下错误消息。
- 在“;”之前应有“)”令牌(if 行)
- 期望在“)”之前进行主表达式 令牌(if 行)
- else 没有前面的 if(仅用于最后一个 else)
我在 C++ 方面确实很新,从示例来看,我的一切看起来都是正确的。如有帮助,将不胜感激。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串后面需要一个分号,而
if
条件内不需要分号。另外,运算符是
<=
,而不是=<
。You need a semicolon after your strings and don't need one inside the
if
conditions.Also, the operator is
<=
, not=<
.不要将分号放在“()”括号中。另一方面,您必须以分号结束每条指令(并且您没有在第一个“{}”括号中这样做)。它应该是:
顺便说一句,如果您在 if 或 else 之后使用简单的指令,则不必使用括号,也可以是:
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:
BTW, you don't have to use brackets if you use a simple instruction after if or else, it can be as well:
<=
(认为小于或等于)而不是=<
if (表达式) { [first-statement-when-true; [第二...;]] } else { [第一个语句当假时; [第二...;]] }
这足以让它工作,但是
std::string
的默认构造函数本身无论如何都是“”,所以你不需要else { error_i = ""; } } 代码...或者,您可以捕获原始代码的这种或那种感觉,并立即为
error_i
指定一个有意义的值,如下所示:上面,
?
和:
根据先前表达式的真值(n <= 0
)选择它们之间或之后的值,然后将其赋值为error_i
的初始值。有些人会认为这很糟糕,因为将std::string
设置为""
毫无意义,而且可能效率稍低(无论如何,它们在默认构造后都是空的),但是清楚地表达你对程序的想法的代码通常更重要。<=
(think less than or equal) rather than=<
if (expression) { [first-statement-when-true; [second...;]] } else { [first-statement-when-false; [second...;]] }
That's enough to get it to work, but
std::string
s default-constructor themselves to be "" anyway, so you don't need theelse { error_i = ""; }
code at all...OR, you can capture the this-or-that feel of your original and immediately give
error_i
a meaningful value as in: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 oferror_i
. Some people would think this is bad because setting astd::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.