Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

burtonlang

macrumors newbie
Original poster
Apr 5, 2008
20
0
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, 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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors newbie
Dec 24, 2004
20
0
Claremont, CA
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

macrumors newbie
Original poster
Apr 5, 2008
20
0
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.
 

psingh01

macrumors 68000
Apr 19, 2004
1,586
629
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

macrumors newbie
Original poster
Apr 5, 2008
20
0
Okay, everything is working. Thanks a lot to everyone who replied.
Alex.
 

DataThief

macrumors member
Jul 30, 2007
86
0
Somewere in Washington State
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.