【问题标题】:Getting errors stray ‘\342’ and ‘\200’ and ‘\214’ [duplicate]得到错误偏离'\342'和'\200'和'\214'[重复]
【发布时间】:2013-12-06 09:54:23
【问题描述】:

我正在尝试为一个简单的游戏编写一个程序,但我遇到了错误,在 Ubuntu 13.10 中使用 g++ 和 gedit 时出现错误“\342”、“\200”和“\214”(俏皮蝾螈) .

代码是:

#include <iostream>
#include <cctype>

using namespace std;

char all_d()
{
    return 'D';
}

int main()
{
    bool more = true;
    while ( more )
    {
        cout << "enter C to cooperate, D to defect, Q to quit\n";
        char player_choice;
        cin >>‌ player_choice;

        if ( player_choice != 'C' || player_choice != 'D' || player_choice != 'Q' )
        {
            cout << "Illegal input.\nenter an other\n";
            cin >> player_choice;
        }

        char  cpu_choice = all_d();

        cout << "player's choice is " << player_choice << endl;
        cout << "cpu's choice is " << cpu_choice << endl;
    }

    if ( player_choice == 'Q' )
    {
        cout << "Game is Over!\n";
        more = false;
    }
}

终端输出为:

IPD.cpp:18:3: error: stray ‘\342’ in program
   cin >>‌ player_choice;
   ^
IPD.cpp:18:3: error: stray ‘\200’ in program
IPD.cpp:18:3: error: stray ‘\214’ in program
IPD.cpp: In function ‘int main()’:
IPD.cpp:29:47: error: ‘end’ was not declared in this scope
   cout << "cpu's choice is " << cpu_choice << end;
                                               ^
IPD.cpp:32:7: error: ‘player_choice’ was not declared in this scope
  if ( player_choice == 'Q' )
       ^

我什至试图编译这个:

#include <iostream>

using namespace std;

int main()
{
    char a;
    cin >>‌ a;
}

终端又说:

a.cpp:8:2: error: stray ‘\342’ in program
  cin >>‌ a;
  ^
a.cpp:8:2: error: stray ‘\200’ in program
a.cpp:8:2: error: stray ‘\214’ in program

我该如何解决这个问题?

请注意,我昨晚安装了 Ubuntu。

【问题讨论】:

  • cout &lt;&lt; ... &lt;&lt; end?替换为endl
  • 你用的是什么编辑器?
  • 尝试使用不会在源代码中添加愚蠢字符的编辑器。或者至少使用一个编辑器来向您显示那些愚蠢的角色​​。
  • 342 200 214(八进制)→ 0xE2 0x80 0x8C(十六进制)→ Unicode 代码点 U+200C (ZERO WIDTH NON-JOINER) 的 UTF-8 序列。

标签: c++ ubuntu g++


【解决方案1】:

您在代码中的 &gt;&gt; 之后使用了 Zero Width Non Joiner 字符。这是一个 Unicode 字符,以 UTF-8 编码为三个字符,其值为 0x2e0x800x8c(或以 8 为基数,\342\200\214)。这可能是因为您从使用这些特殊字符的文档(HTML 网页?)中复制并粘贴了一些代码。

C++ 语言要求整个程序主要使用 ASCII 编码,除了字符串或字符文字的内容(可能是依赖于实现的编码)。因此,要解决您的问题,请确保您只使用简单的 ASCII 空格、引号、双引号,而不是智能字符。

【讨论】:

  • "除了字符串或字符文字的内容" C++还允许在cmets中使用非ASCII字符。
  • FWIW,\342 对我来说是花哨的引号字符 (不是 ASCII 单引号 '
【解决方案2】:
cin >>‌ a;

我已将其复制粘贴到 Python 中,发现在 &gt;&gt; 和以下空格之间有 3 个字符:\xe2 \x80 \x8c。将它们解码为 UTF-8,你会得到一个ZERO WIDTH NON-JOINER

它是如何进入您的代码的,我不知道。找出来。您使用的编辑器有什么有趣的地方吗?你的键盘布局?

【讨论】:

  • 一个非常常见的原因是从网页、PDF 文档以及通过聊天(例如 Skype 聊天)(格式化文本源)复制代码。
  • Re “我已将其复制粘贴到 Python 中”:你到底做了什么?
【解决方案3】:

除了所说的之外,您还有一个与在其范围之外使用变量 player_choice 有关的错误。

while ( more )
{
    cout << "enter C to cooperate, D to defect, Q to quit\n";
    char player_choice;
    // other stuff
}

if ( player_choice == 'Q' )
{
    cout << "Game is Over!\n";
    more = false;
}

在上面的代码sn-p之前的while循环中定义,退出循环后被销毁。所以在这个 if 语句中使用它是非法的。

【讨论】:

    【解决方案4】:

    我复制粘贴了您的代码 sn-p,完全未修改,发现您在 cin &gt;&gt; 之后有一个非常时髦的空格字符。往下看:

    去掉&lt;200c&gt;,你应该可以正常编译了。根据您使用的编辑器,您可能会也可能不会看到它像这样打印出来。

    【讨论】:

    • 里面的“”是什么? if 中的第二个错误呢?
    • 这是我的编辑器表示零宽度非连接字符的方式。 fileformat.info/info/unicode/char/200c/index.htm 就其余错误而言,您有一些语法问题需要修复(例如,end 是什么?)但这是最大的问题,因为它很难看到。
    • 你的编辑是什么?
    • @PeterMortensen 在我回答这个问题时,很可能是 mvim
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多