PDA

View Full Version : C++ Programming with XCode 2




novicew
Jan 5, 2006, 11:54 AM
I am pretty new to Apple and trying to write my first c++ program on XCode platform. However due to it's complexity I am kind of stuck not knowing from where to start with. I would really appreciate if someone can explain me how to do that.

#include<iostream>
using namespace std;
int main()
{
cout <<"Hello World!\n";
cin.get();
}

By the way, I had no problem running it on Terminal.

Thank you in advance.



mwpeters8182
Jan 5, 2006, 01:05 PM
I am pretty new to Apple and trying to write my first c++ program on XCode platform. However due to it's complexity I am kind of stuck not knowing from where to start with. I would really appreciate if someone can explain me how to do that.

#include<iostream>
using namespace std;
int main()
{
cout <<"Hello World!\n";
cin.get();
}

By the way, I had no problem running it on Terminal.

Thank you in advance.

When you get started in XCode, you want to choose new project, then "C++ Tool" under "Command Line Utility". This will create a project that is of the type you're looking to create. main.cpp even has hello world already in it to start.

novicew
Jan 5, 2006, 01:30 PM
I managed to get that far too. My problem is to execute some of the *.cpp files I have already written on Windows. Is there a way to just open a *.cpp file, compile it and execute it in the same platform.

Thank you for the help anyway

Catfish_Man
Jan 5, 2006, 02:30 PM
I managed to get that far too. My problem is to execute some of the *.cpp files I have already written on Windows. Is there a way to just open a *.cpp file, compile it and execute it in the same platform.

Thank you for the help anyway

Use gcc in terminal, like you said originally... that's the easiest way to compile simple CLI C++ programs. Xcode is designed for larger stuff.

slooksterPSV
Jan 7, 2006, 11:28 PM
Use gcc in terminal, like you said originally... that's the easiest way to compile simple CLI C++ programs. Xcode is designed for larger stuff.
CLI - Command Line Interface - and ummm as long as you don't need a sepcific library like SDL (that one you can just use XCode to set it up and that) -the lib files- then just use the terminals CLI if you know it. Otherwise, maybe I can get Dev-C++ for Linux working or something on Mac. XCode is great though.

bousozoku
Jan 8, 2006, 03:46 AM
I managed to get that far too. My problem is to execute some of the *.cpp files I have already written on Windows. Is there a way to just open a *.cpp file, compile it and execute it in the same platform.

Thank you for the help anyway

Depending on your project, you can just copy and paste the text into the main.cpp file since it's already known to be the main file. Otherwise, you should copy the files into the directory and add them to the project and then, build the project.

Soulstorm
Jan 8, 2006, 04:06 AM
I managed to get that far too. My problem is to execute some of the *.cpp files I have already written on Windows. Is there a way to just open a *.cpp file, compile it and execute it in the same platform.

Thank you for the help anyway
Since you have already done that, I suppose that can compile hello world application and run it, but there are some programs written on the PC that just don't compile.

That is because there are some differences in some PC Commands: for example:
system("PAUSE");will not work, because it is a Windows specific subroutine and not C++ specific. Another command that won't work is clrscr();This is also windows specific. There are other commands like these. OS X only supports commands that are specified in ANSI C++and not those windows specific sub-routines.

bousozoku
Jan 8, 2006, 04:15 AM
Since you have already done that, I suppose that can compile hello world application and run it, but there are some programs written on the PC that just don't compile.

That is because there are some differences in some PC Commands: for example:
system("PAUSE");will not work, because it is a Windows specific subroutine and not C++ specific. Another command that won't work is clrscr();This is also windows specific. There are other commands like these. OS X only supports commands that are specified in ANSI C++and not those windows specific sub-routines.

Many of those include C header files. I believe conio.h is one of those such header files, but it's been a long time since I did any DOS programming.

It would be easy enough to do a pause programme but if u'r not already using a shell to execute the programme directly, I'm not sure what the output would be. Of course, C++ doesn't really offer native unbuffered input, so you would have to resort to C (fgetc using stdin would probably be a good choice) to halt execution.

novicew
Jan 25, 2006, 08:20 AM
Can you figure out what I am doing wrong here? (http://osxdaily.blogspot.com/2006/01/cerror.html).

Thank you in advance

mwpeters8182
Jan 25, 2006, 08:42 AM
can you post the actual errors from the compiler?

Also, in the class, I think you'll want to have width and height be private.

Soulstorm
Jan 25, 2006, 08:50 AM
compiles fine on me. You fail to mention what kind of errors are those 6 arrors that you get...

Josh
Jan 25, 2006, 09:24 AM
The one thing I noticed when using XCode for C++ that was different from using the terminal (g++) or MS Visual Studio, is that XCode requires me to use:

std::cout << whatver is here

Rather than just
cout << whatver is here.

I'm sure there are plenty other differences I will notice as I go on, but just started my class last week and this is what I've noticed so far.

Soulstorm
Jan 25, 2006, 10:01 AM
The one thing I noticed when using XCode for C++ that was different from using the terminal (g++) or MS Visual Studio, is that XCode requires me to use:

std::cout << whatver is here

Rather than just
cout << whatver is here.

I'm sure there are plenty other differences I will notice as I go on, but just started my class last week and this is what I've noticed so far.
that's just the namespace definition.
at the top of the program, use "use namespace std;" and it will not require you to do so. Perhaps you should read more about namespaces.

Josh
Jan 25, 2006, 11:11 AM
Thanks for the info.

Perhaps I should :p

Sogo
Jan 25, 2006, 03:54 PM
Ok how do i compile main.cpp using the terminal? This is a c++ program.

Oh, and how do i get it to run...

CookieMook
Jan 25, 2006, 04:37 PM
Ok how do i compile main.cpp using the terminal? This is a c++ program.

Oh, and how do i get it to run...

type g++ <yourCppFilename.cpp> in the terminal. It should produce an application (executable file) if all is well. Just type the name of the application and it should run.

MacCoaster
Jan 25, 2006, 07:31 PM
CookieMook: close... If you don't provide the -o argument, it'll simply create the binary executable named "a.out".
g++ -o myprogram myprogram.cpp
What's even better, learn makefiles. They'll take care of compilation easier than manually compiling.

novicew: why do you have the last two lines uncommented. delete those or comment those out. like:// rect area: 12
// rectb area: 30
and just some tip for you with your area() method, you might want to make it const like:int area() const; // in the class scope
and move the actual implementation as:int CRectangle::area() const { return width * height; }this way you can guarantee the area() method will NOT mutate the width or height variables.

Though I'd suggest you move the CRectangle:: stuff to a .cpp file and the class declaration in a header file. Xcode will automatically compile and link them all together.

A final suggestion, use 'unsigned int' because neither width nor height can be negative. if you explictly try to add a negative integer the compiler will complain, however if you simply pass from input, it won't check, so you'll have to do the checking code if you use the user's input. So:
#include <iostream>

using namespace std;

class CRectangle {
unsigned int width, height;
public:
CRectangle( unsigned int w, unsigned h );
unsigned int area() const;
};

CRectangle::CRectangle( unsigned int w, unsigned int h ) {
width = w;
height = h;
}

unsigned int CRectangle::area() const {
return width * height;
}

int main( int argc, char* argv[] ) {
CRectangle rectA( 3, 4 );
CRectangle rectB( 5, 6 );

cout << "rectA area: " << rectA.area() << endl;
cout << "rectB area: " << rectB.area() << endl;

return 0;
}

// rectA area: 12
// rectB area: 30
compiles great with g++ -o foo foo.cpp

novicew
Jan 26, 2006, 09:26 AM
Hi MacCoaster,

Thank you for the valuable tips. Now it seems to be working fine.:)