C++ If 和 if else 错误消息

发布于 2024-11-06 11:43:05 字数 616 浏览 1 评论 0原文

我正在制作数学函数的集合。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静若繁花 2024-11-13 11:43:05

字符串后面需要一个分号,而 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 = "";}

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 = "";}
意中人 2024-11-13 11:43:05

不要将分号放在“()”括号中。另一方面,您必须以分号结束每条指令(并且您没有在第一个“{}”括号中这样做)。它应该是:

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

顺便说一句,如果您在 if 或 else 之后使用简单的指令,则不必使用括号,也可以是:

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

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 = "";
情栀口红 2024-11-13 11:43:05
string error_i;
if (num1=<0;) {error_i = "Sorry, can't do imaginary numbers."}
else {error_i = "";} 
  • 您想要的比较运算符是 <= (认为小于或等于)而不是 =<
  • 带括号的表达式不需要分号,但语句需要
    • 此处 if (表达式) { [first-statement-when-true; [第二...;]] } else { [第一个语句当假时; [第二...;]] }

这足以让它工作,但是 std::string 的默认构造函数本身无论如何都是“”,所以你不需要else { error_i = ""; } } 代码...

string error_i;

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

或者,您可以捕获原始代码的这种或那种感觉,并立即为 error_i 指定一个有意义的值,如下所示:

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

上面,? 根据先前表达式的真值(n <= 0)选择它们之间或之后的值,然后将其赋值为error_i 的初始值。有些人会认为这很糟糕,因为将 std::string 设置为 "" 毫无意义,而且可能效率稍低(无论如何,它们在默认构造后都是空的),但是清楚地表达你对程序的想法的代码通常更重要。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文