-2

I'm at a university and some teachers send us C++ code using PDF files.

When I want to copy the code from the PDF to Code::Blocks and run the program, I get some weird errors, like: "stray '\342' in program" or "stray '\200' in program" or other codes like this where 342 or 200 could be any number made out of 3 digits.

How can I copy code from a PDF to Code::Blocks and make it work?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Substitute the invalid characters to corresponding valid characters using your favorite text editor? – MikeCAT Apr 20 '21 at 12:35
  • Or, hopefully, you are using a modern operating system that has commands like `tr`, that will do the search and replace, or character deletion for you, with a minimum of fuss? – Sam Varshavchik Apr 20 '21 at 12:38
  • 1
    You might have some special characters in pdf (as Non-breaking space). – Jarod42 Apr 20 '21 at 12:38
  • 5
    Copying code is a bad habit. If you type the code in by hand, your fingers will soon learn what correct code feels like, and you will make fewer errors. (And write faster.) – molbdnilo Apr 20 '21 at 12:41
  • pdf seems odd, it's not like a plaintext source file will ever be larger. – sweenish Apr 20 '21 at 12:43
  • Related: *[Compilation error: stray ‘\302’ in program, etc](https://stackoverflow.com/questions/19198332/)* – Peter Mortensen Apr 25 '23 at 19:35
  • I think this is a very relevant question for Stack Overflow. But it is very likely a duplicate. This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 25 '23 at 19:44
  • cont' - There may also be other canonical questions for various tools for detection and mitigation. Sample: *[How to show non-ASCII characters in Visual Studio Code](https://stackoverflow.com/questions/68557261/)* – Peter Mortensen Apr 25 '23 at 20:10

1 Answers1

1

Paste it in some code formatter in between. PDF files seem to mess up spaces. After that, you can just copy it back to your IDE.

However, maybe it was your teacher's intention that you should copy the code by hand?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lasersköld
  • 2,028
  • 14
  • 20
  • Well, I don't know if it was his intention, but we have a time to copy all the code and to run it and sometimes that time is not enough, I don't get to copy the whole code, to run it and to send the results back to him and if we say to him that we don't have enough time, well, he doesn't care:(( – alinrabulea Apr 20 '21 at 13:00
  • Fair enough. Not all teachers are perfect. – Lasersköld Apr 20 '21 at 13:04
  • 1
    @alinrabulea Wait... Your teacher sends you code as pdf, you convert it to plain text, compile and execute it, and send back the results? Are you supposed to learn anything useful from this? – molbdnilo Apr 20 '21 at 15:27
  • @alinrabulea I would say that is up to the person asking the question. Maybe that is how the teacher sends code to their students. – Lasersköld Apr 21 '21 at 08:18
  • 1
    @molbdnilo It's not a subject about coding, it's a subject about math, but involves a bit of coding too, the subject is numerical analysis, but we don't do very advanced stuff because I'm in the first year at university and we still learn the basics and thats why the teacher send us the code in PDF. Theoretically, we should save some time, but in reality we only waste it. And I'm sorry I responded so late, but I didn't open the site for a while. – alinrabulea Apr 27 '21 at 12:33
  • My guess is that the teacher is [blissfully](https://en.wiktionary.org/wiki/ignorance_is_bliss#Proverb) unaware of the problems he/she is creating by transferring code through PDF documents. – Peter Mortensen Apr 25 '23 at 21:49