getting errors stray ‘\342’ and ‘\200’ and ‘\214’

2019-01-15 16:33发布

This question already has an answer here:

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:

#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 ‘\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' )
       ^

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 ‘\342’ in program
  cin >>‌ a;
  ^
a.cpp:8:2: error: stray ‘\200’ in program
a.cpp:8:2: error: stray ‘\214’ in program

can anyone help me with that?

note that I was installed ubuntu last night.

标签: c++ ubuntu g++
4条回答
看我几分像从前
2楼-- · 2019-01-15 17:06
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.

How it got into your code, I don't know. Find it out. Anything interesting about the editor you're using? Your keyboard layout?

查看更多
不美不萌又怎样
3楼-- · 2019-01-15 17:16

You are using a Zero Width Non Joiner character 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.

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.

查看更多
疯言疯语
4楼-- · 2019-01-15 17:22

Apart from what was said you have also one more error related to using variable player_choice outside its scope.

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-15 17:22

I copy pasted your code snippet, completely unmodified, and found that you have a seriously funky space character after cin >>. Look below:

enter image description here

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.

查看更多
登录 后发表回答