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

Discussion in 'Mac Programming' started by burtonlang, Apr 5, 2008.

  1. macrumors newbie

    Joined:
    Apr 5, 2008
    #1
    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:
    and the errors are as follows:
    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.
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    change end1 to endl (the last character is a lowercase L). It might need to be std::endl or add using namespace std;

    -Lee
     
  3. macrumors newbie

    Joined:
    Dec 24, 2004
    Location:
    Claremont, CA
    #3
    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
     
  4. thread starter macrumors newbie

    Joined:
    Apr 5, 2008
    #4
    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.
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    change the end to another std::endl on line 8.
     
  6. macrumors 65816

    Joined:
    Apr 19, 2004
    #6
    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".
     
  7. thread starter macrumors newbie

    Joined:
    Apr 5, 2008
    #7
    Okay, everything is working. Thanks a lot to everyone who replied.
    Alex.
     
  8. macrumors member

    Joined:
    Jul 30, 2007
    Location:
    Somewere in Washington State
    #8
    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
     

Share This Page