New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

XCode/C++ Help


Post Reply New Thread Subscribe

 
Thread Tools
Healey

 
Member Since: Jun 07, 2010
Posts: 3
Healey is on a distinguished road

Healey is offline
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!
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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.
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,092
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
+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; 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 Debian as a server; Apple TV (1080p version)
QUOTE Thanks
Healey

 
Member Since: Jun 07, 2010
Posts: 3
Healey is on a distinguished road

Healey is offline
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++?
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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.
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,092
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
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; 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 Debian as a server; Apple TV (1080p version)
QUOTE Thanks
Healey

 
Member Since: Jun 07, 2010
Posts: 3
Healey is on a distinguished road

Healey is offline
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.
QUOTE Thanks
gurghet

 
Member Since: Jun 13, 2010
Posts: 2
gurghet is on a distinguished road

gurghet is offline
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.
QUOTE Thanks
Bill McEnaney

 
Member Since: May 27, 2010
Posts: 21
Bill McEnaney is on a distinguished road

Bill McEnaney is offline
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;
}
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,092
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
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; 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 Debian as a server; Apple TV (1080p version)
QUOTE Thanks

Post Reply New Thread Subscribe


« Launching an application while executing an applescript? | Weird Python or Shell issue »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

All times are GMT -4. The time now is 05:20 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?