Pasting code


#1

Hi,

I’m pasting code in from the book and am getting some strange behavior in xcode. Pasting in the ‘turkey’ program yields this:

Invalid preprocessing directive
Unknown type name 'i’
Expected identifier or ‘(’

Those are errors on the
#include and int line.

The code color formatting also does not take effect until I retype the code. Very strange.

Thanks for any suggestions you have.


#2

Pasting between applications can sometimes yield wonky results. Would you mind pasting it here (using the code tag) so that we can see?

As to the syntax highlighting, it tends to break down when there’s invalid code in the document. So fixing the first problem ought to solve the second. :slight_smile:


#3

[code]#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
​ ​ ​ ​/​/​ ​D​e​c​l​a​r​e​ ​t​h​e​ ​v​a​r​i​a​b​l​e​ ​c​a​l​l​e​d​ ​’​w​e​i​g​h​t​’​ ​o​f​ ​t​y​p​e​ ​f​l​o​a​t​
​ ​ ​ ​f​l​o​a​t​ ​w​e​i​g​h​t​;​

​ ​ ​ ​/​/​ ​P​u​t​ ​a​ ​n​u​m​b​e​r​ ​i​n​ ​t​h​a​t​ ​v​a​r​i​a​b​l​e​
​ ​ ​ ​w​e​i​g​h​t​ ​=​ ​1​4​.​2​;​

​ ​ ​ ​/​/​ ​L​o​g​ ​i​t​ ​t​o​ ​t​h​e​ ​u​s​e​r​
​ ​ ​ ​p​r​i​n​t​f​(​"​T​h​e​ ​t​u​r​k​e​y​ ​w​e​i​g​h​s​ ​%​f​.​\​n​"​,​ ​w​e​i​g​h​t​)​;​

​ ​ ​ ​/​/​ ​D​e​c​l​a​r​e​ ​a​n​o​t​h​e​r​ ​v​a​r​i​a​b​l​e​ ​o​f​ ​t​y​p​e​ ​f​l​o​a​t​
​ ​ ​ ​f​l​o​a​t​ ​c​o​o​k​i​n​g​T​i​m​e​;​

​ ​ ​ ​/​/​ ​C​a​l​c​u​l​a​t​e​ ​t​h​e​ ​c​o​o​k​i​n​g​ ​t​i​m​e​ ​a​n​d​ ​s​t​o​r​e​ ​i​t​ ​i​n​ ​t​h​e​ ​v​a​r​i​a​b​l​e​
​ ​ ​ ​/​/​ ​I​n​ ​t​h​i​s​ ​c​a​s​e​,​ ​’​​’​ ​m​e​a​n​s​ ​’​m​u​l​t​i​p​l​i​e​d​ ​b​y​’​
​ ​ ​ ​c​o​o​k​i​n​g​T​i​m​e​ ​=​ ​1​5​.​0​ ​+​ ​1​5​.​0​ ​
​ ​w​e​i​g​h​t​;​

​ ​ ​ ​/​/​ ​L​o​g​ ​t​h​a​t​ ​t​o​ ​t​h​e​ ​u​s​e​r​
​ ​ ​ ​p​r​i​n​t​f​(​"​C​o​o​k​ ​i​t​ ​f​o​r​ ​%​f​ ​m​i​n​u​t​e​s​.​\​n​"​,​ ​c​o​o​k​i​n​g​T​i​m​e​)​;​

​ ​ ​ ​/​/​ ​E​n​d​ ​t​h​i​s​ ​f​u​n​c​t​i​o​n​ ​a​n​d​ ​i​n​d​i​c​a​t​e​ ​s​u​c​c​e​s​s​

​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​[/code]


#4

When I paste into textmate, there are spaces between every letter. Copied from mysafaribooksonline - perhaps this is some kind of copy protection?


#5

Yep, there’s the problem.

When I paste that code into Xcode, it looks normal. It also looks normal in SublimeText (my text editor of choice). I imagine it looks normal in most true text editors.

But what happens when I paste that text into an app with word-processing capabilities, such as a new TextEdit document:

It turns out that there’s whitespace between every character that isn’t a typical space, because it’s not recognized by all text editors. I chalk this up to being an artifact of how the pdf/ePub/etc. was either generated or is being interpreted by the application it’s been opened with.


#6

Are there code samples available for the book? I don’t see any on the safari books site. Thanks for your help.


#7

Here is the workaround

support.safaribooksonline.com/view?id=1030