C++ in XCode 4, coding from Windows book

Discussion in 'Mac Programming' started by terrapin, Mar 21, 2011.

  1. terrapin macrumors newbie

    Joined:
    Mar 21, 2011
    #1
    Hello all, I just got the new 13-inch MBP having moved from Windows Vista on a POS HP machine. I had done some basic C++ programming a few years ago, but quickly gave up because of other factors (schooling, etc.) Now that I have more free to time to be able to dedicate to learning this language, I have a few questions since some of the programs I wrote into the Xcode Command Line Tool for C++ aren't working, already.

    Now in this book I'm using, which is meant for Windows, it gives an example of a good first program to write:

    #include <iostream>
    #include <stdlib.h>

    int main (int argc, const cha * argv[])
    {

    cout << "Hello, I am your computer talking." << endl;
    system("PAUSE");
    return 0;

    }




    In Xcode 4, the Hello world example is this:

    #include <iostream>

    int main (int argc, const char * argv[])
    {

    // insert code here...
    std::cout << "Hello, World!\n";
    return 0;
    }

    I am already noticing some differences. Obviously I am new, and don't understand the differences between C++ in a UNIX environment vs. on a Windows environment. The first thing I can tell is different is the lack of "#include <stdlib.h>" line on the Mac example. The second thing I can tell is different is the "std::cout" vs just "cout" on the Windows example program. Why do I need std:: before the cout identifier? When I ran the Windows example without "std::" right before cout or endl I noticed that the compiler had a red exclamation mark with "Use of undeclared identifier 'cout' " nonetheless.

    I want to learn C++ on my Mac and develop some programming understanding and was wondering if anyone could help me with the above problems. Really thanks a million in advance, this stuff is very interesting to me but I think I've reached the upper limit of my own problem solving.

    Thanks!!! :apple: :D
     
  2. jiminaus, Mar 21, 2011
    Last edited: Mar 21, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    The #include <stdlib.h> in your Windows example is there because that code uses the system function which is declared in stdlib.h. The hello world code XCode 4 started you off with doesn't use the system function, so doesn't need to include stdlib.h.

    BTW system("PAUSE") will only work under Windows.

    Your Windows code example isn't strictly C++ standards compliant. In an archaic, pre-standard version of C++ you could just code cout. The C++ standard says cout is declared in the std namespace. This means you now either need to put std:: before it, like in the Mac example, or include a "using namespace std;". The same applies to endl.

    I would suggest getting a C++ book that is standards-compiant and that doesn't assume Windows. But as many recommend here. You should actually start with C before C++.
     
  3. bux macrumors regular

    Joined:
    Apr 22, 2004
    Location:
    Sweden
    #3
    EDIT: too slow on posting, but I keep what I wrote :)

    The reason for std before cout and endl is because it's inside the std namespace which you must declare like that or tell that you are using that namespace.

    Like:
    using namespace std;

    or (I think?) to not include all namespace:
    using std::cout;
    using std::endl;

    int main(...) {...
    now cout and endl doesn't need std::

    and the system("pause") must be some windows thing, which I assume will make it wait for a key input or something.

    Also in c++ if you want to include stdlib.h you should actually be using:
    #include <cstdlib>

    if you want to follow the standards, now I haven't been coding c++ in a long time so someone correct me where I am wrong :)
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    The missing "using namespace std;" in the example is worrisome. terrapin can you tell us which book.

    I'm really of two minds about that recommendation.

    In general I think it is easier to go from OOP back to procedures than vice-versa, so getting a handle on objects as early as possible is quite useful if the ultimate goal is to use an object oriented language.

    As with Obj-C, I think a good approach is to start down the OOP path and divert midstream to bolster up C skills.

    B
     
  5. kuwisdelu macrumors 65816

    Joined:
    Jan 13, 2008
    #5
    This.

    The others have already told you the differences, so I won't repeat them. But your book isn't using standard C++. Get a new one.

    This, too, but not quite as important.

    I think procedural vs. object-oriented isn't that big a deal w.r.t. which you jump into first, but for C dialects, I think learning about pointers in pure C first can save a lot of frustration and confusion later.
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5)

    Pointer abuse is actually one if the reasons to use C++ or Objective C first as many of the things pointers are used for in C have cleaner implementations in classes for things like strings and arrays.

    B
     
  7. terrapin thread starter macrumors newbie

    Joined:
    Mar 21, 2011
    #7
    C++ All in One Desk Reference (for Dummies)

    This is the book I am currently using. It is indeed quite old and I even remember a couple years back having troubles installing the requisite "library files" to get the compiler to compile properly. It is rather troubling that it would omit

    The first big suggestion (other than first learning C) was getting a "standards-compliant C++ book that doesn't assume Windows." This seems logical enough. Can anyone suggest a good book? I did use the search function as well as Google but in such a sea of information I believe a little direction would serve me well. I am having a hard time understanding why a book (even a for Dummies book) wouldn't be "standards compliant."

    Here is another program I wrote in Xcode based on code in the Windows book:


    #include <iostream>

    int main (int argc, const char * argv[])
    {
    use namespace std;
    use namespace endl;
    int first, second;
    std::cout << "Dividing 28 by 14." << std::endl;
    first = 28;
    second = 14;
    std::cout << "Quotient " << first / second << std::endl;
    std::cout << "Remainder " << first % second << std::endl;
    std::cout << "Dividing 32 by 6." << std::endl;
    first = 32;
    second = 6,
    std::cout << "Quotient " << first / second << std::endl;
    std::cout << "Remainder " << first % second << std::endl;

    return 0;
    }


    It is not working. On the fourth line that begins with "std::cout..." it says "Thread 1: stopped at breakpoint 1" and I tried messing with the "\n" that I saw in Xcode examples within quotes (I don't know that it means) as well as with std::endl but nothing seems to be working correctly.
     
  8. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    1. There is no need to try to declare namespaces; beginners tend to do that because writing std::cout instead of cout is so much more work, but in a large program it is really useful to see the full name std::cout and to be sure that it means what you think it means. I see you are using std::cout and std::endl anyway, so that is fine.

    2. It seems you are using a debugger and you are stopping at a breakpoint. Command-Shift-Y shows the debugger window if it isn't visible yet. Then you have buttons to step over the next statement, to step out of a function, or to just continue running. Very useful when you first write a program and want to test it. For example, you could _see_ the values first and second changing as you execute statement by statement.
     
  9. terrapin, Mar 21, 2011
    Last edited: Mar 21, 2011

    terrapin thread starter macrumors newbie

    Joined:
    Mar 21, 2011
    #9
    Here's a screenshot, I just don't understand what it doesn't like. I understand your first point about namespaces (kinda) but my debugger is in fact open... here's a screenshot, the debugger is in the bottom left corner

    [​IMG]
     
  10. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    The blue flag above the int main line is a breakpoint. It stops on the line below because that is the first line of executable code it hits.

    This is another almost religious debate. When first learning programming, an IDE like Xcode or Visual Studio can get in the way as you are learning the fine points of the language and the IDE at the same time. You may want to learn how to compile, link and run code outside the IDE first.

    B
     
  11. terrapin thread starter macrumors newbie

    Joined:
    Mar 21, 2011
    #11
    I don't mind using a less complicated complier for now and migrating to Xcod once I've gotten a grasp of C++. Do you have any recommendations for a standards based C++ book that doesn't assume you're running windows?
     
  12. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #12
    Not really. You could use the books by (The C++ Programming Language) or Prata (C++ Primer Plus) but I haven't looked at either in over 10 years.

    Personally I'd recommend a book that introduces and uses STL early, and I can't think of one that is aimed at beginners.

    B
     
  13. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #13
  14. terrapin, Mar 21, 2011
    Last edited: Mar 21, 2011

    terrapin thread starter macrumors newbie

    Joined:
    Mar 21, 2011
    #14
    I read some reviews, looked at the book, and read through chapter zero. Two questions. I did his first example, which is written as follows (VERBATIM):


    // a small C++ program
    #include <iostream>
    int main()
    {
    std::cout


    He says in the first chapter (Chapter 0) "alright, so this should give you "Hello world""

    What? :confused: There is no ";" after the std::cout, nor is there a "}" on the line under the std::cout. There isn't even a line in the example that says "Hello world"

    Also, this book was last updated in 2000, which is 3 years before the last stable release of the C++ standard. Is it ok to learn a programming language from a resource written prior to the latest stable release? Perhaps this is why the example is loopy (for me)
     
  15. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #15
    Unfortunately this book does not seem to be available in electronic form from legitimate sources. (No Kindle, Nook, iBooks, Google Books or Safari Online). Don't steal books. Buy it or find a local library that has a copy if you can't buy it.

    Look at the code tarball from the authors' site http://www.acceleratedcpp.com/

    This should be the code example from Chapter 0.

    Code:
    // a small C++ program
    #include <iostream>
    
    int main()
    {
    	std::cout << "Hello, world!" << std::endl;
    	return 0;
    }
    
    If you absolutely must use a free resource, consider "Thinking in C++" which is distributed in HTML here: http://nyll.com/ebooks/index.html among many other mirrors.

    B
     
  16. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #16
    I'd like to point out that the problem isn't that this book is not standards compliant--it's that it adheres to a very old standard. The C++ that everyone in here is familiar with was set in 1998. When I first learned C++ (also in 1998), namespaces were not part of the actively used standard. Also, neither were virtual functions. It takes a while for compilers to be updated. Visual Studio 2003 had issues with some of the more complicated uses of templates.
     
  17. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #17
    Funny you should mention it.

    I dug out my copy of Practical C++ Programming by Oualline (http://oreilly.com/catalog/9781565921399/) which was from that vintage (1995 though mine was printed and purchased in 1997) and it has the same "Hello World" issues as the OP's book. No using namespace, no std::

    While the second edition from 2002 adds the std:: I would expect today according to the sample code downloaded from the book's site.

    I didn't recommend "Practical C++ Programming" because I knew it was out of date, I just didn't remember in what ways.

    B
     
  18. terrapin thread starter macrumors newbie

    Joined:
    Mar 21, 2011
    #18

    So I guess the issue is that the book is outdated and because I'm using software (Xcode) just released it isn't compatible with the older standard. Got it. I basically just need any more recent book. I'll go with Accelerated C++
     
  19. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #19

Share This Page