6

I'm trying to write a program for a simple game, but I am getting errors, stray ‘\342’ and ‘\200’ and ‘\214’, using g++ and gedit in Ubuntu 13.10 (Saucy Salamander).

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 output 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' )
       ^

I even tried to to compile this:

#include <iostream>

using namespace std;

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

and the terminal again says:

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

How can I fix this?

Note that I installed Ubuntu last night.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ArMor
  • 109
  • 1
  • 2
  • 8
  • `cout << ... << end`? replace with `endl` – Benoit Dec 06 '13 at 09:55
  • 2
    what editor are you using? – Kos Dec 06 '13 at 09:56
  • Try using an editor that doesn't add silly characters to your source code. Or at least use an editor that **shows** you the silly characters when they are there. – john Dec 06 '13 at 09:58
  • 342 200 214 (octal) → 0xE2 0x80 0x8C (hexadecimal) → UTF-8 sequence for Unicode code point U+200C ([ZERO WIDTH NON-JOINER](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8704&number=128)). – Peter Mortensen Mar 06 '21 at 22:00
  • @john: It may not be the editor, but caused by copying the code from a web page, say a blog post. – Peter Mortensen Jan 16 '23 at 13:43

4 Answers4

15

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 0xE2, 0x80, and 0x8C (or in base 8, \342, \200, and \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 requires 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85
2
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?

Kos
  • 70,399
  • 25
  • 169
  • 233
2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

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.

yamafontes
  • 5,552
  • 1
  • 18
  • 18
  • what is "<2000>" in there? and what about the second error in if? – ArMor Dec 06 '13 at 10:08
  • It's the way my editor represents a zero-width non joiner character. http://www.fileformat.info/info/unicode/char/200c/index.htm As far as the rest of your errors go, you have a few syntax things that need to be fixed (like, what is `end`?) but this one is the biggest problem cause it's hard to see. – yamafontes Dec 06 '13 at 10:09
  • What is your editor? – Peter Mortensen Mar 05 '21 at 06:18
  • 1
    @PeterMortensen at the time i answered this, most likely mvim – yamafontes Mar 19 '21 at 03:17