17

I'm getting these errors in my program after pasting in some code:

showdata.cpp:66: error: stray ‘\342’ in program
showdata.cpp:66: error: stray ‘\200’ in program
showdata.cpp:66: error: stray ‘\235’ in program
showdata.cpp:66: error: stray ‘\’ in program
showdata.cpp:66: error: stray ‘\342’ in program
showdata.cpp:66: error: stray ‘\200’ in program
showdata.cpp:66: error: stray ‘\235’ in program
showdata.cpp:67: error: stray ‘\342’ in program
showdata.cpp:67: error: stray ‘\200’ in program
showdata.cpp:67: error: stray ‘\235’ in program
showdata.cpp:67: error: stray ‘\’ in program
showdata.cpp:67: error: stray ‘\342’ in program
showdata.cpp:67: error: stray ‘\200’ in program
showdata.cpp:67: error: stray ‘\235’ in program

Here are the two lines that are causing the errors.

size_t startpos = str.find_first_not_of(” \t”);
size_t endpos = str.find_last_not_of(” \t”);

How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
neuromancer
  • 53,769
  • 78
  • 166
  • 223
  • A much more direct analysis is 342 200 235 (octal) → 0xE2 0x80 0x9D (hexadecimal) → UTF-8 sequence for Unicode code point U+201D ([RIGHT DOUBLE QUOTATION MARK](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)). Most text editors (e.g. [Geany](https://en.wikipedia.org/wiki/Geany) (Linux and Windows) and [Notepad++](https://en.wikipedia.org/wiki/Notepad%2B%2B)) with a [regular expression](https://en.wikipedia.org/wiki/Regular_expression) mode will be able to do search/replace for Unicode code point U+201D, using `\x{201D}`. – Peter Mortensen Mar 06 '21 at 21:05
  • 5
    Does this answer your question? [Compilation error: stray ‘\302’ in program, etc](https://stackoverflow.com/questions/19198332/compilation-error-stray-302-in-program-etc) – ChrisMM Mar 08 '21 at 14:26

4 Answers4

48

The symbol is not ". Those are called 'smart quotes' and are usually found in rich documents or blogs.

LiraNuna
  • 64,916
  • 15
  • 117
  • 140
  • 9
    I think some one is using Word as an IDE ;) – leppie Feb 26 '10 at 10:48
  • 12
    Or copy-pasting code from blogs. Whatever it is, it's not our place to criticise it. – LiraNuna Feb 26 '10 at 10:59
  • It happened to me... I had my old Turbo/Borland C (3.1) code documented on Word. To check if some of them were compatible with GCC/G++ I pasted selected portions from the DOC file to GEdit. And there it was... And the worst part is that I never noticed this change until I saw this answer... Hmmm maybe I'm getting old :) – itsols Apr 20 '13 at 14:12
  • This happens when you use "shift+enter" in some editors – Ari Nov 20 '17 at 22:42
  • It is Unicode code point U+201D ([RIGHT DOUBLE QUOTATION MARK](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)). Most text editors (e.g. [Geany](https://en.wikipedia.org/wiki/Geany) (Linux and Windows) and [Notepad++](https://en.wikipedia.org/wiki/Notepad%2B%2B)) with a [regular expression](https://en.wikipedia.org/wiki/Regular_expression) mode will be able to do search/replace for Unicode code point U+201D, using `\x{201D}`. – Peter Mortensen Mar 06 '21 at 21:09
6

The lines

 size_t startpos = str.find_first_not_of(” \t”); 
 size_t endpos = str.find_last_not_of(” \t”); 

have some "special" kind of double quotes, try the following:

 size_t startpos = str.find_first_not_of(" \t"); 
 size_t endpos = str.find_last_not_of(" \t"); 
hlovdal
  • 26,565
  • 10
  • 94
  • 165
4

This sort of error message, error: stray ‘\xyz’ in program, can appear with any other character or symbol that is not recognized by the compiler as a legal one.

Sharing my personal experience:

 - bool less<const char∗>(const char∗ a, const char∗ b)
 - bool less<const char*>(const char* a, const char* b)

The former one is copy-pasted from a PDF file. It doesn't compile.

The latter one compiles as expected.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
0

You can use the sed command to fix these issues.

This will give you a quick preview of what will be replaced.

sed s/[”“]/'"'/g File.txt

This will do the replacements and put the replacement in a new file called WithoutSmartQuotes.txt.

sed s/[”“]/'"'/g File.txt > WithoutSmartQuotes.txt

This will overwrite the original file.

sed -i ".bk" s/[”“]/'"'/g File.txt

cokedude
  • 379
  • 1
  • 11
  • 21
  • I don't think you should have a space between `-i` and `.bk` - GNU sed (at least) will interpret them separately (i.e. "in-place with no backup file" and a command that's a syntax error). – Toby Speight Mar 01 '17 at 17:37
  • How do you type the slanted and inverted slanted double quotes on a keyboard? (in linux) – Ribo Jan 10 '19 at 16:08