C++ 获取错误 '\342' 和 '\200' 和 '\214'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20420890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting errors stray ‘\342’ and ‘\200’ and ‘\214’
提问by ArMor
hi I'm trying to write a program for a simple game but getting errors stray ‘\342' and ‘\200' and ‘\214' using g++ and gedit in ubuntu 13.10. the code is:
嗨,我正在尝试为一个简单的游戏编写一个程序,但是在 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;
}
}
and terminal out put is:
和终端输出是:
IPD.cpp:18:3: error: stray ‘2' in program
cin >>? player_choice;
^
IPD.cpp:18:3: error: stray ‘0' in program
IPD.cpp:18:3: error: stray ‘4' 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' )
^
even tried to to compile this:
甚至试图编译这个:
#include <iostream>
using namespace std;
int main()
{
char a;
cin >>? a;
}
and terminal says again:
终端再次说:
a.cpp:8:2: error: stray ‘2' in program
cin >>? a;
^
a.cpp:8:2: error: stray ‘0' in program
a.cpp:8:2: error: stray ‘4' in program
can anyone help me with that?
任何人都可以帮助我吗?
note that I was installed ubuntu last night.
请注意,我昨晚安装了 ubuntu。
回答by Sylvain Defresne
You are using a Zero Width Non Joinercharacter after the >>
in your code. This is a unicode character that is encoded in UTF-8 as three characters with values 0x2e
, 0x80
, 0x8c
(or in base 8, \342
, \200
, \214
). This probably happened because you copy and pasted some code from a document (html web page?) that uses those special characters.
您在代码中使用了零宽度非连接字符>>
。这是一个 unicode 字符,以 UTF-8 编码为三个字符,值分别为0x2e
, 0x80
, 0x8c
(或以 8 为基数,\342
, \200
, \214
)。这可能是因为您从使用这些特殊字符的文档(html 网页?)中复制并粘贴了一些代码。
The C++ language require that the whole program uses mostly the ASCII encoding, except for the content of strings or character literals (that may be in a implementation-dependent encoding). So to fix your problem, make sure that you only use simple ASCII space, quotes, double quotes and not smart characters.
C++ 语言要求整个程序主要使用 ASCII 编码,除了字符串或字符文字的内容(可能采用依赖于实现的编码)。因此,要解决您的问题,请确保仅使用简单的 ASCII 空格、引号、双引号而不是智能字符。
回答by Vlad from Moscow
Apart from what was said you have also one more error related to using variable player_choice outside its scope.
除了所说的之外,您还有一个与在其范围之外使用变量 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;
}
It was defined in the while loop before the code snippet above and was destroyed after exiting the loop. So using it in this if statement is illegal.
它是在上面代码片段之前的 while 循环中定义的,并在退出循环后被销毁。所以在这个 if 语句中使用它是非法的。
回答by Kos
cin >>? a;
I've copy-pasted that into Python and found out that between >>
and the following space there are 3 characters: \xe2
\x80
\x8c
. Decode them as UTF-8 and you get a ZERO WIDTH NON-JOINER.
我已将其复制粘贴到 Python 中,并发现在>>
以下空格之间有 3 个字符:\xe2
\x80
\x8c
. 将它们解码为 UTF-8,你会得到一个零宽度非连接器。
How it got into your code, I don't know. Find it out. Anything interesting about the editor you're using? Your keyboard layout?
我不知道它是如何进入你的代码的。找出来。您正在使用的编辑器有什么有趣的地方吗?你的键盘布局?
回答by yamafontes
I copy pasted your code snippet, completely unmodified, and found that you have a seriously funky space character after cin >>
. Look below:
我复制粘贴了您的代码片段,完全未修改,并发现您在cin >>
. 往下看:
Get rid of the <200c>
, and you should be able to compile just fine. Depending on the editor you're using, you may or may not be able to see it printed like this.
去掉<200c>
,你应该能够编译得很好。根据您使用的编辑器,您可能会或可能无法看到它像这样打印。