Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jalaska13

macrumors newbie
Original poster
Mar 21, 2009
24
0
Hi guys!
I've been trying to teach myself C++ from a book that is written for PC users (most are). I simply skipped the steps it says about installing the compiler that comes with the book, and figured i'd use XCode. After all, C++ is the same, no matter the platform. However, i'm having a bit of difficulty. When I try to type in the code examples they give, and then compile and run them, all the "build" and "run" commands are greyed out in the pulldown menus. All i have is a simple C++ file (.cpp). No headers, no make files, nothing. Just that little bit of code. What am I doing wrong?
here's the code:

#include <iostream>
using namespace std;

int main() {
cout << "Never fear, C++ is here!";
return 0;
}
 
Choose File->New Project
Under Command Line Utility, choose C++ tool

Now you can enter your code.

This will set up the environment for the sort of thing you want to do, so it knows what compiler to use, etc.

-Lee
 
I've just recently started learning C++ on XCode myself and have attempted to make a simple program. My code is:

#include <iostream>
#include <string>
using namespace std ;

int main (void)
{
string name ;

cout << "Enter you full name. " ;
getline(cin,name);

cout << "Hello, " << name << endl ;

return 0 ;
}

Before it asks me to enter my name, I see all this stuff in the console window:


The Debugger has exited with status 0.
[Session started at 2009-10-19 11:44:11 -0400.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1346) (Fri Sep 18 20:40:51 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001
Loading program into debugger…
Program loaded.
run
[Switching to process 1213]
Running…


I was wondering if there's a way to get rid of that ^?

When I enter my name, I get this message:

MyProgram(1213) malloc: *** error for object 0x1000041e0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all


Clearly there's an error and I was wondering what is wrong with my code?
 
When I enter my name, I get this message:

MyProgram(1213) malloc: *** error for object 0x1000041e0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all


Clearly there's an error and I was wondering what is wrong with my code?

Well, it gives you a bloody big hint what to do if you want to debug your code.
 
considering the fact that i'm new to this and i got this code from a website to test out xcode how am i to know what that jibberish means?
 
I tried googling '*** set a breakpoint in malloc_error_break to debug' and one result told me to hit 'alt+cmd+B' and then near the blue cube icon to input 'malloc_error_break' i tried that and it didn't work.
 
cin >> name;

instead of getline stuff you have

I registered just to reply to this. I am not sure why he is trying to explain much more advanced debugging or whatever rather than just point out the obvious mistake.

malloc is memory allocation stuff. I am still a student so I cannot really tell you much more.

Oh and you do not need void inside of int main()
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.