C++ in XCode 4, coding from Windows book

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:

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.

The first thing I can tell is different is the lack of "#include <stdlib.h>" line on the Mac example.

Click to expand...

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.

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?

Click to expand...

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++.

Staff Member

But as many recommend here. You should actually start with C before C++.

Click to expand...

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.

I would suggest getting a C++ book that is standards-compiant and that doesn't assume Windows.

Click to expand...

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.

Quote

But as many recommend here. You should actually start with C before C++.

Click to expand...

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.

I think learning about pointers in pure C first can save a lot of frustration and confusion later.

Click to expand...

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.

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:

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.

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.

Click to expand...

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.

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.

Click to expand...

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

Staff Member

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.

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

Click to expand...

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?

Staff Member

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?

Click to expand...

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.

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? 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)

Staff Member

I read some reviews, downloaded the book, and read through chapter zero.

Click to expand...

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.

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.

Staff Member

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.

Click to expand...

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.

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

Click to expand...

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++

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.