0

I get this while trying to compile my C++ program

error: stray ‘\342’ in program
���func("a","b",3,0);
^

But when I paste from Atom (the text editor I use) I get this ⁠⁠⁠

  func("a","b",3,0);

I need to add that I pasted this from WhatsApp web and there are 40 lines like this that would take way too long to rewrite. I would like to know what and where the bad character is.

More code:

  func("a","b",3,0);
  func("c","d",1,3);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
carzacc
  • 21
  • 4
  • Googling the error message led me to a good selection of sources saying smart quotes are to blame. It took me barely any time to check 3 sources. – chris Sep 19 '17 at 15:44
  • 1
    The bad quotes are not an issue in my case, it would take you barely any time to check my code and see that I'm using the correct kind of quote (I spent time Googling before posting this and most people were using these quotes “” instead of these "" and my keyboard can't even make that kind of wrong quotes) – carzacc Sep 19 '17 at 15:50
  • By the way, the UTF-8 representation of a left smart "double" quote is 11100010 10000000 10011100. Notice how the first octet matches octal 342 exactly. It's likely that the compiler also issued errors for the other two octets: \200 and \234. Either way, it's harder to tell without a [mcve], preferably with a link to an online compiler reproducing the errors. – chris Sep 19 '17 at 15:50
  • I can also tell you that my IDE confirms that I'm using the correct quotes(If I search them by copy-pasting it finds every single "double" quote I've used and the others aren't giving me errors) – carzacc Sep 19 '17 at 15:52
  • Anyway, it's not easy to look at your question and tell because the only non-ASCII-range characters in your posted code are the �s. There's nothing *in* the code that I can actually copy to figure out which character it really is because none of the bytes you've pasted match the \342 byte, not to mention the rest of the likely UTF-8 character. More succinctly, the code in the question does not produce this error. – chris Sep 19 '17 at 15:56
  • Edited my post adding a few more lines of code, in case the backspace between would help (I've only edited the function name and the strings between the quotes because I don't think that's what would make a difference) – carzacc Sep 19 '17 at 16:12
  • To my utter surprise, changing the function name to func and back to the original one fixed it. How could this work? – carzacc Sep 19 '17 at 16:16
  • 1
    The close reason is incorrect. This is a duplicate, asked many times over ("Error: stray ‘\342’" makes it quite clear what the problem is (is entirely reproducible and is ***not*** caused by a typo)). The canonical is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Mar 05 '21 at 05:19

1 Answers1

2

You are answering your question by yourself. The error shows you, that the first 3 whitespaces are causing the trouble. To fix this, try something like regex or the "search and replace" function to replace the "wrong" whitespaces with the correct ones.

xMutzelx
  • 566
  • 8
  • 22
  • How can whitespace be "wrong", isn't whitespace supposed to be all the same?How can I replace wrong whitespace with correct whitespace? – carzacc Sep 19 '17 at 14:39
  • And I also have deleted all of the whitespace preceding that line and (using VSCode since that uses spaces instead of the TAB character) replaced all of it, and the compiler still gives the same output – carzacc Sep 19 '17 at 14:45
  • Now, with NO whitespace before every line I still get the same output – carzacc Sep 19 '17 at 14:49
  • 1
    Use a non-Unicode editor, delete and re-type the line. Error could be on the preceding line if strange unicode characters have got into your file. – Richard Critten Sep 19 '17 at 14:51
  • @carzacc Regarding your first comment: Just copy the "wrong" whitespace into the "search and replace" function of your IDE and replace it with the right withspace (press space). This can happen when the encoding is not the same. Regarding your second comment: Is is possible that you don't use a common encoding like ASCII? If you replaced it with your IDE and you still got the error you should check this. – xMutzelx Sep 19 '17 at 14:54
  • @RichardCritten what would be a non-Unicode editor? – carzacc Sep 19 '17 at 15:11
  • @xMutzelx Not only I've done that, now there is NO whitespace between the start of a line and the code and I still get the same error – carzacc Sep 19 '17 at 15:13
  • 1
    @carzacc Please notice the comment from Richard. Maybe the error is in the preceding line. If you also have deleted the whitespaces there, you should update your post with the new error message. Maybe something else is wrong. – xMutzelx Sep 19 '17 at 15:51
  • I get the same error message every time, I've tried deleting the whitespace in the preceding line and nothing changes – carzacc Sep 19 '17 at 15:58
  • @carzacc try using `vi` or any more recent edit that lets you select the encoding for the current file (choose ASCII). Fallback is create a new file and very carefully copy and paste into it missing out the area around the problem (+-3 lines should be enough) then retype those lines. Compilers are only really happy with ASCII files and what is white-space will differ between you and the compiler when using Unicode. – Richard Critten Sep 19 '17 at 15:58
  • It is likely U+200B ([ZERO WIDTH SPACE](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)). It can be searched/replaced using a regular expression search with `\x{200B}`. – Peter Mortensen Mar 06 '21 at 19:29