PDA

View Full Version : C++ Noob: can't compile code straight from book




burtonlang
Apr 5, 2008, 07:13 PM
I have just started learning C++ from the book "SAMS Teach Yourself C++ in 21 Days" and the very first exercise (after Hello World) won't compile in g++. I've checked to make sure I wasn't making any typos or anything, and I have definitely typed exactly what it says in the book.

Here is the code:
#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << end1;
std::cout << x + y << " " << x * y;
std::cout << end;
return 0;
}

and the errors are as follows:
Untitled.cpp: In function `int main()':
Untitled.cpp:6: error: 'end1' was not declared in this scope
Untitled.cpp:8: error: 'end' was not declared in this scope


Does it have something to do with the compiler, or maybe with my system (Mac OS X 10.4.11 running on a 800 MHz PowerPC G4 iMac)?

I saw this same question by a Linux user somewhere else on the internet (http://bytes.com/forum/thread642497.html), and it seems that the consensus there is that the book has made an error. Though this is possible, I have heard many good things about this book from multiple sources, and I kind of doubt that this is the case.

Anyway, I hope somebody can help me and reply soon.
Thanks in advance,
Alex.



lee1210
Apr 5, 2008, 07:42 PM
change end1 to endl (the last character is a lowercase L). It might need to be std::endl or add using namespace std;

-Lee

Minkintosh
Apr 5, 2008, 07:44 PM
end1 needs to be endl

it's "L" not "1"

and std::cout is correct but, if you use namespace std then you do not have to type std:: all the time

burtonlang
Apr 5, 2008, 08:22 PM
Thank you so much. I fixed what you told me was wrong and now I have:
#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << std::endl;
std::cout << x + y << " " << x * y;
std::cout << std::end;
return 0;
}

but I still have errors:
Untitled.cpp: In function `int main()':
Untitled.cpp:8: error: 'end' is not a member of 'std'


Is "end" supposed to "endl" too? That seems logical, and it compiles, but why are two of the same line necessary?

Because I've hardly even gotten into this book, I am not sure how to use "namespace std;" but I don't know if that would actually fix anything, if it's just an alternative (of course I know nothing).

Thank you so much,
Alex.

lee1210
Apr 5, 2008, 08:48 PM
change the end to another std::endl on line 8.

psingh01
Apr 5, 2008, 09:02 PM
Thank you so much. I fixed what you told me was wrong and now I have:


but I still have errors:


Is "end" supposed to "endl" too? That seems logical, and it compiles, but why are two of the same line necessary?

Because I've hardly even gotten into this book, I am not sure how to use "namespace std;" but I don't know if that would actually fix anything, if it's just an alternative (of course I know nothing).

Thank you so much,
Alex.

That line just prints an empty line. They used it twice to print two empty lines. The reason it is necessary is because whoever wrote the code wanted it there. lol no other reason :)

And yes it needs to be "endl" and not "end".

burtonlang
Apr 5, 2008, 09:15 PM
Okay, everything is working. Thanks a lot to everyone who replied.
Alex.

DataThief
Apr 5, 2008, 11:30 PM
Just a note to find any more errors that crop up, the number 8 in this line

Untitled.cpp:8: error: 'end' was not declared in this scope

is a good starting point on where to look first for the error, ie in line 8