嗨我想写一个简单的游戏程序,但在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; } }
而terminal输出是:
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; }
terminal又说:
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。
您在代码中的>>
后面使用零宽度非连接符字符。 这是一个unicode字符,以UTF-8编码为值为0x8c
, 0x8c
(或在基本8, \342
, \200
, \214
)中的三个字符。 这可能是因为您复制并粘贴了使用这些特殊字符的文档(html网页?)中的一些代码。
C ++语言要求整个程序主要使用ASCII编码,除了字符串或字符文字的内容(可能与实现相关的编码)。 所以要解决您的问题,请确保您只使用简单的ASCII空间,引号,双引号而不是智能字符。
cin >> a;
我已经复制粘贴到Python中,发现>>
和下面的空间之间有3个字符: \xe2
\x80
\x8c
。 将它们解码为UTF-8,并获得零宽度非连接器 。
它如何进入你的代码,我不知道。 找出来。 关于您正在使用的编辑器的任何有趣的事情? 你的键盘布局?
除了所说的话之外,还有一个与在其范围之外使用变量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; }
它是在上面的代码片段之前的while循环中定义的,并在退出循环后被销毁。 所以在这个if语句中使用它是非法的。
我复制粘贴你的代码片段,完全没有修改,发现你有一个严重时髦的空间字符后cin >>
。 往下看:
摆脱<200c>
,你应该能够编译得很好。 根据您使用的编辑器,您可能会或可能无法看到它打印像这样。