Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting errors stray ‘\342’ and ‘\200’ and ‘\214’ [duplicate]

Tags:

c++

g++

ubuntu

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.

like image 343
ArMor Avatar asked Dec 06 '13 09:12

ArMor


People also ask

How do I fix error 342 on stray?

The problem is that you have Unicode quotation marks instead of ASCII quotation marks; probably your editor automatically changed them, or you copied the text from a site that does this automatically in its authoring software. Replace the quotes with normal ASCII quote (0x22, ") and it should work.

How do I fix stray errors?

Go to the line which is reported and you see the straying special char. That happens if you had opend or edited you sourcecode with an editor that enters “non breakable spaces” instead of the common whitespace. That might happen if you push “shift-space” instead of only “space”, depending on the editor you use.

What is stray error in C++?

Error: stray '\240' in program is a character encoding error message. \240 is a non-breaking space in iso8859-1 encoding used on web pages, especially when the code shouldn't get line-wrapped by the browser. The compiler doesn't understand non-breaking space and reports about a straying special character error.

What is stray error in Arduino?

Why you get Stray errors? If you are having a stray error on the compilation of code, itis most likely that you have copied the code from any website which has Unicode characters.


1 Answers

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 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.

like image 60
Sylvain Defresne Avatar answered Oct 14 '22 08:10

Sylvain Defresne