Results 1 to 10 of 10

Thread: XCode/C++ Help

  1. #1


    Member Since
    Jun 07, 2010
    Posts
    3
    XCode/C++ Help
    So I'm a day-old noob at XCode and I don't know a thing about it. I bought "C++ for Dummies" the other day, and it wants me to use "codeblocks" as a code editor and compiler, but it's being very difficult for me. So I installed the developer package off of my Leopard disc, and I'm totally lost. XCode seems way more powerful/flexible than codeblocks, and I have no idea where to start, so I'll give you the code I want to compile, and hopefully you guys can tell me how to make it XCode compatible, and what to open it with/where to put it.

    Code:
    //Conversion- program to convert celsius to fahrenheit
    #include <cstdio>
    
    #include <cstdlib>
    #include <iostream>
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    	//enter temp in celsius
    	int celsius;
    	cout << "Enter the tempreture in Celsius:";
    	cin >> celsius
    	
    	//calculate conversion factor from celcius to fahr.
    	int factor;
    	factor = 212 - 32;
    	
    	//use conversion factor to convert celcious to far.
    	int fahrenheit
    	fahrenheit = factor * celsius/100 + 32
    	
    	//show results
    	cout << "fahrenheit value is:";
    	cout << fahrenheit << endl;
    	
    	//wait until user has read results
    	system("Pause");
    	return 0;
    }
    So yeah. Help is appreciated.
    Thanks!

  2. #2

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    You want to build a command line tool project and choose C++ as the type.

    If your intent is to learn to program for Mac OS X or iOS (iPhone), then C++ isn't the language to start with. Also, I won't be surprised if you run into problems with that book since it may be geared towards Windows (I noticed on Amazon that it mentioned Visual C++ help).
    CameraTime - Time lapse photography for novice and advanced users.

    When asking questions, post the version of your software. You'll receive better answers.

    Please post your results to the thread as it is good feedback.

  3. #3


    Member Since
    Feb 25, 2009
    Posts
    2,112
    Specs:
    Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
    +1 for what xstep said. If your goal is for OSX or iOS, you need to get yourself a book on Objective C like this one: Amazon.com: Programming in Objective-C 2.0 (2nd Edition) (9780321566157): Stephen G. Kochan: Books (the third edition is due out in Sept, otherwise I would recommend the 3rd edition). The one nice thing about that Objective-C book is it really doesn't assume you have existing C knowledge (of course, for those of us that do have C experience, there is quite a bit that can be skimmed over rather then read in detail)

    C++ for dummies (original or 2009 edition) does allow the use of the gnu compiler (which you also have on your system with the install of xcode - g++ - you'd just have to use some sort of editor to create your files then compile them on the command line - a tool like vim or JEdit (I happen to like jedit for syntax highlighting) to edit the files), but it is more geared to you ultimately programming on a windows based machine (if you look at the naming conventions of the variables, that's more windows standard of prefixing variables with identifiers to help remind you what the variable is - like n is for int, psz is a pointer to a string, etc.
    My Macs: Late 2013 rMBP w/ 750m, 16Gig ram; 2013 Mac Pro 6 core w/ D700, 16Gig Ram; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Linux; Apple TV (1080p version)

  4. #4


    Member Since
    Jun 07, 2010
    Posts
    3
    All right. Thanks for the help. Too bad I'm down 30 bucks now. I want to learn both Mac and Windows programming, but using a Mac as my preferred computer, I will be leaning towards Mac. I plan on contributing to open-source Mac compatible games and software, as well as debugging, and maybe even making my own game one day, though that's far off. With that information about what I plan to do, do you have any different advice about what programming path to follow?

    In the meantime, when building the project I'm getting errors on three lines due to improper code, which I'm sure is because the book is teaching me a different variation. They are:

    Code:
    cout << "Enter the tempreture in Celsius:";
    
    reason-"cout" not declared in scope
    Code:
    cin >> celsius;
    
    reason-"cin" not declared in scope
    Code:
    cout << fahrenheit << endl;
    
    reason-"endl" not declared in scope
    "cout" is used(or I was taught) to output given information, and "cin" is to allow the user to input information. "endl" is used to end the line.

    So if someone could translate, that would be awesome.

    Thanks again!

    EDIT: Oh yeah, what's the difference between Objective C, C, and C++?

  5. #5

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    I don't know how to answer the scope issue because I don't know C++. I would suspect you haven't included some file. Double check you code to the book.

    Learning more than one system at a time is tough. I'd say get the Kochan book and learn Objective-C first, then move to a Cocoa or iOS (iPhone) book. Once you learn one language well, you should be able to pick up others easily enough. The big thing now days is learning the frameworks such as Cocoa. They are big and have many capabilities.

    As for Windows, if I was going to learn that, I think I'd go with C# and Microsoft's Visual Studio Express. A Windows coder will be able to advise you better.
    CameraTime - Time lapse photography for novice and advanced users.

    When asking questions, post the version of your software. You'll receive better answers.

    Please post your results to the thread as it is good feedback.

  6. #6


    Member Since
    Feb 25, 2009
    Posts
    2,112
    Specs:
    Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
    Actually, you have several bugs:


    1) In C++, they have what are called namespaces - it's what allows multiple groupings of classes to have the same actual name for a given class and still be allowed to used within a program (ie: if you wanted to write your own cout class, you'd have to create it within a namespace you create, then you would reference your cout class by namespace, so it would be called mynamespace::cout) In the situation here, the easiest thing to do is to tell the compiler which namespace you plan to use for all function calls there on out. Cout and cin are part of the std namespace. For example, to call cout without announcing which namespace you want prior you would need to call it as std::cout. To quickly solve the problem for your program, before main, but after the includes add a line:

    using namespace std;

    2) You're missing a lot of semicolons. Each instruction (except for a very few that you haven't encountered yet, ie: if/then statements since a ';' at the end of the if line would cause the "then" portion to not be conditionally activated and instead run each time the if test code is executed) requires a ';' at the end.

    Looking at your code, see my marked areas:

    Code:
    //enter temp in celsius
    	int celsius;
    	cout << "Enter the tempreture in Celsius:";
    ----->cin >> celsius
    	
    	//calculate conversion factor from celcius to fahr.
    	int factor;
    	factor = 212 - 32;
    	
    	//use conversion factor to convert celcious to far.
    ----->int fahrenheit
    ----->fahrenheit = factor * celsius/100 + 32
    	
    	//show results
    	cout << "fahrenheit value is:";
    	cout << fahrenheit << endl;
    	
    	//wait until user has read results
    	system("Pause");
    	return 0;
    each missing semicolon will cause the compiler to fail on compile and return an error.

    Hope that helps.
    My Macs: Late 2013 rMBP w/ 750m, 16Gig ram; 2013 Mac Pro 6 core w/ D700, 16Gig Ram; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Linux; Apple TV (1080p version)

  7. #7


    Member Since
    Jun 07, 2010
    Posts
    3
    Thanks, the namespace std fixed it, and the program works, but after it is done in the console is displays "sh: Pause: command not found" and the the console ends. So that means that command is probably Windows specific.

    I don't plan on developing for the iPhone, so I don't think I would want to get caught up in that. I'm not diehard Mac either, and because I'm very young, later on in life I want to be able to have many different programming options, not just Mac.

    Thank you guys very much for the feedback, I'll sit on this for a while and decide in the next couple of weeks where I should start.

  8. #8


    Member Since
    Jun 13, 2010
    Posts
    2
    Just saw the "pause" command.
    That's not part of any standard so you cannot use it safely.
    In MS-DOS however, it should prompt something like:
    "Press any key to continue..."
    Never found the any key.

  9. #9


    Member Since
    May 27, 2010
    Posts
    21
    I think you a need to end each marked line with a semicolon.
    Code:
    //Conversion- program to convert celsius to fahrenheit
    #include <cstdio>
    
    #include <cstdlib>
    #include <iostream>
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    	//enter temp in celsius
    	int celsius;
    	cout << "Enter the tempreture in Celsius:";
    	cin >> celsius //Semicolon here?
    	
    	//calculate conversion factor from celcius to fahr.
    	int factor;
    	factor = 212 - 32;
    	
    	//use conversion factor to convert celcious to far.
    	int fahrenheit
    	fahrenheit = factor * celsius/100 + 32 //Semicolon here?
    	
    	//show results
    	cout << "fahrenheit value is:";
    	cout << fahrenheit << endl;
    	
    	//wait until user has read results
    	system("Pause");
    	return 0;
    }

  10. #10


    Member Since
    Feb 25, 2009
    Posts
    2,112
    Specs:
    Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
    Little late in the game there Bill hehe - I already marked the missing semi's and the OP was questioning the command not found issue which was answered by gurghet regarding "pause" being a DOS command and not a OSX command.
    My Macs: Late 2013 rMBP w/ 750m, 16Gig ram; 2013 Mac Pro 6 core w/ D700, 16Gig Ram; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Linux; Apple TV (1080p version)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Xcode stopped letting me run Xcode app on iPad
    By ildsarria in forum iOS Development
    Replies: 1
    Last Post: 11-09-2011, 03:15 PM
  2. XCode 4 and C++
    By applefan90 in forum OS X - Development and Darwin
    Replies: 4
    Last Post: 03-22-2011, 01:12 AM
  3. Xcode use help
    By chat19 in forum OS X - Development and Darwin
    Replies: 8
    Last Post: 05-26-2008, 08:12 AM
  4. Xcode 1.5
    By alejandrico in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 06-03-2006, 05:56 PM
  5. XCode
    By meskue in forum OS X - Development and Darwin
    Replies: 1
    Last Post: 05-21-2006, 02:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •