What is wrong with this sample code from my C++ book?

DougoMan

Senior member
May 23, 2009
813
0
71
So I decided to read a book and learn a little about C++, and every stupid example results in errors while compiling.


// carrots.cpp -- food processing program
// uses and displays a variable

// carrots.cpp -- food processing program
// uses and displays a variable
#include <iostream>
int main()
{
using namespace std;
int carrots; // declare an integer variable
carrots = 25; // assign a value to the variable
cout << &#8220;I have &#8220;; <--- error in this line
cout << carrots; // display the value of the variable
cout << &#8220; carrots.&#8221;; <----error in this line
cout << endl;
carrots = carrots - 1; // modify the variable
cout << &#8220;Crunch, crunch. Now I have &#8220; << carrots << &#8220; carrots.&#8221; << endl; <---error...
return 0;
}




Not off the a good start here...

Tried using Microsoft and Bloodshed compilers.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
You've got some character encoding issues. Instead of copying and pasting the code try typing it in your self. It's a good thing to do anyways... the best way to learn how to write code is to practice.
 

DougoMan

Senior member
May 23, 2009
813
0
71
You've got some character encoding issues. Instead of copying and pasting the code try typing it in your self. It's a good thing to do anyways... the best way to learn how to write code is to practice.

I'll try that, but what do you mean by character encoding issues?

Isn't a character a character?


edit: It did work :) Page 2, here I come.
 
Last edited:

Merad

Platinum Member
May 31, 2010
2,586
19
81
It's the quotes around the strings: &#8220; (book) vs " (expected). The first is just a different style of quotation mark, and thus a different character than what the compiler expects.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Interesting that they put the "using namespace std;" declaration inside the main function. Usually you see that as a global declaration for the file and not a local one for a function.
 

homercles337

Diamond Member
Dec 29, 2004
6,340
3
71
Interesting that they put the "using namespace std;" declaration inside the main function. Usually you see that as a global declaration for the file and not a local one for a function.

I was going to say the same thing. I cant think of any case where that would appropriate.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Isn't a character a character?

That is a surprisingly complicated topic, but the short answer is no. When you see a line of text on screen every character is represented by a pattern of bits in memory, or in a file. There are at least three or four distinct, standard patterns of bits that can represent a given character. Google a bit on ASCII, ANSI code pages, and Unicode and it will become clear (sorta).

In this case you just had the wrong type of quote, and compilers are very picky about that sort of typo, fortunately.