PDA

View Full Version : Problem with using strings in Xcode (C++)




Fx1k
Oct 23, 2010, 09:26 PM
First off, I am brand new to Xcode. I have been trying to learn how to program in C++ for a couple of days now.

Whenever I attempt to use the following code:

#include <iostream>
#include <string>


int main(void)
{
using std::cin;
using std::cout;
using std::string;
string name= "";

cout << "What is your name?\n";
cin>> name;
cout << "\nHello " << name.c_str() << "\n";

return 0;
}

I always get the following error message:

What is your name?(1345) malloc: *** error for object 0x100004220: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap
logout

[Process completed]

I have looked around the internet and I have found nothing that has helped me. I found one article that told me how to set the break point, but that didn't work. If anybody knows about this please answer. All answers are appreciated. :D



foidulus
Oct 23, 2010, 10:57 PM
I just tried it in SL on the command line and a command line cpp project and they worked.

How did you create your project?(and what version of XCode are you using?)

lee1210
Oct 23, 2010, 11:10 PM
http://forums.macrumors.com/showthread.php?t=1014767

This comes up all the time. Maybe something at the top of the forum is in order. It comes down to killing Preprocessor macros in the debug build settings.

Also, string knows what to do with <<, so no need to c_str. tags are fun for everyone.

-Lee

Edit: Now there's something in the forum header. Change away if it's not clear.

PatrickCocoa
Oct 23, 2010, 11:26 PM
You didn't ask, but you may want to give up on learning C++ with Xcode.

Why? If you're going to use C++ on Windows, you should learn it on Windows. If you're going to program for the Mac, you'll want to use Objective-C (in general).

subsonix
Oct 24, 2010, 09:32 AM
You didn't ask, but you may want to give up on learning C++ with Xcode.

Why? If you're going to use C++ on Windows, you should learn it on Windows. If you're going to program for the Mac, you'll want to use Objective-C (in general).

MS Windows has nothing to do with this. C++ is an open standard language and there is no reason you can not do standard C++ development on a mac any more than, Ruby, Python, Perl etc. If the aim is to make native os X GUI applications, I agree Cocoa and obj-c is the natural choice, but nothing is being said about that here at all.