【问题标题】:Compilation errors on Codeforces, using MS visual studio 2019 C++Codeforces 上的编译错误,使用 MS Visual Studio 2019 C++
【发布时间】:2020-05-23 17:12:31
【问题描述】:
#include <iostream>

using namespace std;

int main() { //Main Function

int w;
cout << "enter the weight of the watermelon: ";
cin >> w;

if (w <= 1 or w >= 100) {
cout << "error";
}

else {
if (w % 2 == 0) {
cout << "YES";
}
else {
cout << "NO";
}
}
return 0;

编译错误:

 syntax error: missing ')' before identifier 'or'
 error C2065: 'or': undeclared identifier
 error C2146: syntax error: missing ';' before identifier 'w'
 error C2059: syntax error: ')'
 error C2059: syntax error: ';'
 error C2059: syntax error: 'else'
 error C2143: syntax error: missing ';' before '{'
 error C2447: '{': missing function header (old-style formal list?)
 error C2059: syntax error: 'return'
 error C2059: syntax error: '}'
 error C2143: syntax error: missing ';' before '}'

【问题讨论】:

  • main 的右大括号在哪里?无法重现wandbox.org/permlink/uO8SyXIlymbJJZfU
  • 删除return 0;。这种格式使得将左大括号与其对应的右大括号匹配起来非常困难
  • 您是将代码复制粘贴到问题中,还是重写?始终复制粘贴,否则您可能会添加其他不相关的错误,或者更糟的是修复您要询问的错误。
  • 此外,虽然缩进对于语言来说并不重要,但它确实有助于我们人类阅读和理解并遵循代码流程。请学习(始终如一地)缩进您的代码。

标签: c++ visual-c++ compiler-errors syntax-error


【解决方案1】:

or 在 C++ 中是“替代令牌”。

您的 Visual Studio 版本doesn't support these alternative tokens in your compilation mode

从技术上讲,这违反了 C++ 标准。

但是,无论如何,写|| 会更传统,所以就这样做吧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多