Problem with using strings in Xcode (C++)

Discussion in 'Mac Programming' started by Fx1k, Oct 23, 2010.

  1. macrumors newbie

    Oct 23, 2010
    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

    [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
  2. macrumors 6502a

    Jan 15, 2007
    Are you sure you set up the project correctly?

    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?)
  3. macrumors 68040


    Jan 10, 2005
    Dallas, TX

    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. [CODE][/CODE] tags are fun for everyone.


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

    Dec 2, 2008
    Switch gears

    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).
  5. macrumors 68040

    Feb 2, 2008
    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.

Share This Page