• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

DougoMan

Senior member
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.
 
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.
 
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:
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.
 
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.
 
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.
 
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.
 
Back
Top