XCode/C++ Help

Joined
Jun 7, 2010
Messages
3
Reaction score
0
Points
1
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!
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's 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).
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's 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.
 
OP
H
Joined
Jun 7, 2010
Messages
3
Reaction score
0
Points
1
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++?
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's 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.
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's 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.
 
OP
H
Joined
Jun 7, 2010
Messages
3
Reaction score
0
Points
1
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.
 
Joined
Jun 13, 2010
Messages
2
Reaction score
0
Points
1
Just saw the "pause" command. :D
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.
 
Joined
May 27, 2010
Messages
21
Reaction score
0
Points
1
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;
}
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's 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.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top