Display output in C++

I am pretty much a beginner to all programming, but not to computers. I am having trouble displaying the output of a simple program written in C++ using XCode (Leopard).

I started by creating a new project as a Carbon C++ Application. Then I created a C++ file (without the header). My simple program is as follows:


/*
* Program 2.cpp
* Program 2
*
* Created by Dylan on 12/1/07.
* Copyright 2007 _MyCompanyName_. All rights reserved.
*
*/

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
// Without "using statement, this would be std::cout
cout << "Hi there!" << endl; // "endl" = next line
return 0;
}


It is supposed to display "Hi there!" as the output, as I'm sure you all know. I have compiled it successfully but I can't figure out how to display the output. I have searched the built in help for an answer but I can't find it. Could someone point out to me how I can display the results?

Macbook Pro, Mac OS X (10.5)

Posted on Dec 1, 2007 1:34 PM

Reply
4 replies

Dec 1, 2007 4:07 PM in response to dyl123456789

Welcome to Xcode 3.0.
The output console doesn't display by default when the program is running, to make it do so, you have to go in Xcode --> Preferences --> Debugging tab --> On start: local menu.
In that local menu you have the possibility to show the Console when you run the application. After setting the option to Show console you'll always have your output consol displayed.

By the way, why did you create a Carbon C++ program ? A simple command-line tool is enough, in the New Project… assistant, just enlarge Command line utility and select C++ tool.

However if you want to develop an application for Macintosh exclusively, I advise you to use Cocoa instead of Carbon... Carbon is dying, Cocoa is growing.

Dec 2, 2007 3:47 AM in response to dyl123456789

Objective-C is an Object-Oriented Programming language completely based on C, actually it's a thin Object-Oriented layer over C, whereas C++ is a language derived from C but that is now much different. In fact, you can use your whole C code and put it in Objective-C while it's not necessarily the case when putting the code directly in C++.

C++ and Objective-C appeared at the same time but they are slightly different, that's a new language to learn, however if you know C and/or C++, 3/4 of the learning is already made. The major differences reside in the syntax: How to write a method call (in Objective-C it's called a "message", we send messaged to objects), how to write a class, how to write methods, etc.

The difference that made Apple choose this language over C++ for their best API, is certainly because Objective-C is a completely dynamic language, all methods, compared to C++, are supposed "virtual", no non-virtual methods, the language is also completely typed dynamically, it's the runtime that takes care of method resolving and that also allows you to make message forwarding (if an object can't respond a message, it can redirect it to another object that might be able to answer to it).

All of that to say that Objective-C and C++ are not really compatible, you can use Objective-C++ which is a mix up of Objective-C and C++ but there's some restrictions in using it. If you want to develop for Macintosh, I advise you to learn Objective-C and Cocoa, than trying to stay with C++ and Carbon, Objective-C is not a difficult language, especially if you already know C or C++. And also a very pure object-oriented language.

If you want to learn how to program with Objective-C, there's the Apple course : [Object-Oriented Programming with Objective-C|http://developer.apple.com/documentation/Cocoa/Conceptual/OOP ObjC/OOPObjC.pdf]

If you want to learn about Objective-C 2.0 (used in Leopard) there's the link about the whole programming language : [Introduction to The Objective-C 2.0 Programming Language|http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/O bjC.pdf]

And there's the tutorial to program a little application in Cocoa :
[Cocoa Application Tutorial|http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCTutorial /ObjCTutorial.pdf]

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Display output in C++

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.